
import json
import time
import hashlib
from typing import Dict, Any, List, Tuple
import re
from os import environ
import streamlit as st
from langchain.schema import BaseRetriever
from langchain.tools import Tool
from langchain.pydantic_v1 import BaseModel, Field

from sqlalchemy import Column, Text, create_engine, MetaData
from langchain.agents import AgentExecutor
try:
    from sqlalchemy.orm import declarative_base
except ImportError:
    from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from clickhouse_sqlalchemy import (
    types, engines
)
from langchain_experimental.sql.vector_sql import VectorSQLDatabaseChain
from langchain_experimental.retrievers.vector_sql_database import VectorSQLDatabaseChainRetriever
from langchain.utilities.sql_database import SQLDatabase
from langchain.chains import LLMChain
from sqlalchemy import create_engine, MetaData
from langchain.prompts import PromptTemplate, ChatPromptTemplate, \
    SystemMessagePromptTemplate, HumanMessagePromptTemplate
from langchain.prompts.prompt import PromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain.schema import BaseRetriever, Document
from langchain import OpenAI
from langchain.chains.query_constructor.base import AttributeInfo, VirtualColumnName
from langchain.retrievers.self_query.base import SelfQueryRetriever
from langchain.retrievers.self_query.myscale import MyScaleTranslator
from langchain.embeddings import HuggingFaceInstructEmbeddings, SentenceTransformerEmbeddings
from langchain.vectorstores import MyScaleSettings
from chains.arxiv_chains import MyScaleWithoutMetadataJson
from langchain.prompts.prompt import PromptTemplate
from langchain.prompts.chat import MessagesPlaceholder
from langchain.agents.openai_functions_agent.agent_token_buffer_memory import AgentTokenBufferMemory
from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent
from langchain.schema.messages import BaseMessage, HumanMessage, AIMessage, FunctionMessage, \
    SystemMessage, ChatMessage, ToolMessage
from langchain.memory import SQLChatMessageHistory
from langchain.memory.chat_message_histories.sql import \
    DefaultMessageConverter
from langchain.schema.messages import BaseMessage
# from langchain.agents.agent_toolkits import create_retriever_tool
from prompts.arxiv_prompt import combine_prompt_template, _myscale_prompt
from chains.arxiv_chains import ArXivQAwithSourcesChain, ArXivStuffDocumentChain
from chains.arxiv_chains import VectorSQLRetrieveCustomOutputParser
from .json_conv import CustomJSONEncoder

environ['TOKENIZERS_PARALLELISM'] = 'true'
environ['OPENAI_API_BASE'] = st.secrets['OPENAI_API_BASE']

# query_model_name = "gpt-3.5-turbo-instruct"
query_model_name = "gpt-3.5-turbo-instruct"
chat_model_name = "gpt-3.5-turbo-16k"


OPENAI_API_KEY = st.secrets['OPENAI_API_KEY']
OPENAI_API_BASE = st.secrets['OPENAI_API_BASE']
MYSCALE_USER = st.secrets['MYSCALE_USER']
MYSCALE_PASSWORD = st.secrets['MYSCALE_PASSWORD']
MYSCALE_HOST = st.secrets['MYSCALE_HOST']
MYSCALE_PORT = st.secrets['MYSCALE_PORT']
UNSTRUCTURED_API = st.secrets['UNSTRUCTURED_API']

COMBINE_PROMPT = ChatPromptTemplate.from_strings(
    string_messages=[(SystemMessagePromptTemplate, combine_prompt_template),
                     (HumanMessagePromptTemplate, '{question}')])
DEFAULT_SYSTEM_PROMPT = (
    "Do your best to answer the questions. "
    "Feel free to use any tools available to look up "
    "relevant information. Please keep all details in query "
    "when calling search functions."
)


def hint_arxiv():
    st.info("We provides you metadata columns below for query. Please choose a natural expression to describe filters on those columns.\n\n"
            "For example: \n\n"
            "*If you want to search papers with complex filters*:\n\n"
            "- What is a Bayesian network? Please use articles published later than Feb 2018 and with more than 2 categories and whose title like `computer` and must have `cs.CV` in its category.\n\n"
            "*If you want to ask questions based on papers in database*:\n\n"
            "- What is PageRank?\n"
            "- Did Geoffrey Hinton wrote paper about Capsule Neural Networks?\n"
            "- Introduce some applications of GANs published around 2019.\n"
            "- 请根据 2019 年左右的文章介绍一下 GAN 的应用都有哪些\n"
            "- Veuillez présenter les applications du GAN sur la base des articles autour de 2019 ?\n"
            "- Is it possible to synthesize room temperature super conductive material?")


def hint_sql_arxiv():
    st.info("You can retrieve papers with button `Query` or ask questions based on retrieved papers with button `Ask`.", icon='💡')
    st.markdown('''```sql
CREATE TABLE default.ChatArXiv (
    `abstract` String, 
    `id` String, 
    `vector` Array(Float32), 
    `metadata` Object('JSON'), 
    `pubdate` DateTime,
    `title` String,
    `categories` Array(String),
    `authors` Array(String), 
    `comment` String,
    `primary_category` String,
    VECTOR INDEX vec_idx vector TYPE MSTG('fp16_storage=1', 'metric_type=Cosine', 'disk_mode=3'), 
    CONSTRAINT vec_len CHECK length(vector) = 768) 
ENGINE = ReplacingMergeTree ORDER BY id
```''')


def hint_wiki():
    st.info("We provides you metadata columns below for query. Please choose a natural expression to describe filters on those columns.\n\n"
            "For example: \n\n"
            "- Which company did Elon Musk found?\n"
            "- What is Iron Gwazi?\n"
            "- What is a Ring in mathematics?\n"
            "- 苹果的发源地是那里？\n")


def hint_sql_wiki():
    st.info("You can retrieve papers with button `Query` or ask questions based on retrieved papers with button `Ask`.", icon='💡')
    st.markdown('''```sql
CREATE TABLE wiki.Wikipedia (
    `id` String, 
    `title` String, 
    `text` String, 
    `url` String, 
    `wiki_id` UInt64, 
    `views` Float32, 
    `paragraph_id` UInt64, 
    `langs` UInt32, 
    `emb` Array(Float32), 
    VECTOR INDEX vec_idx emb TYPE MSTG('fp16_storage=1', 'metric_type=Cosine', 'disk_mode=3'), 
    CONSTRAINT emb_len CHECK length(emb) = 768) 
ENGINE = ReplacingMergeTree ORDER BY id
```''')


sel_map = {
    'Wikipedia': {
        "database": "wiki",
        "table": "Wikipedia",
        "hint": hint_wiki,
        "hint_sql": hint_sql_wiki,
        "doc_prompt": PromptTemplate(
            input_variables=["page_content",
                             "url", "title", "ref_id", "views"],
            template="Title for Doc #{ref_id}: {title}\n\tviews: {views}\n\tcontent: {page_content}\nSOURCE: {url}"),
        "metadata_cols": [
            AttributeInfo(
                name="title",
                description="title of the wikipedia page",
                type="string",
            ),
            AttributeInfo(
                name="text",
                description="paragraph from this wiki page",
                type="string",
            ),
            AttributeInfo(
                name="views",
                description="number of views",
                type="float"
            ),
        ],
        "must_have_cols": ['id', 'title', 'url', 'text', 'views'],
        "vector_col": "emb",
        "text_col": "text",
        "metadata_col": "metadata",
        "emb_model": lambda: SentenceTransformerEmbeddings(
            model_name='sentence-transformers/paraphrase-multilingual-mpnet-base-v2',),
        "tool_desc": ("search_among_wikipedia", "Searches among Wikipedia and returns related wiki pages"),
    },
    'ArXiv Papers': {
        "database": "default",
        "table": "ChatArXiv",
        "hint": hint_arxiv,
        "hint_sql": hint_sql_arxiv,
        "doc_prompt": PromptTemplate(
            input_variables=["page_content", "id", "title", "ref_id",
                             "authors", "pubdate", "categories"],
            template="Title for Doc #{ref_id}: {title}\n\tAbstract: {page_content}\n\tAuthors: {authors}\n\tDate of Publication: {pubdate}\n\tCategories: {categories}\nSOURCE: {id}"),
        "metadata_cols": [
            AttributeInfo(
                name=VirtualColumnName(name="pubdate"),
                description="The year the paper is published",
                type="timestamp",
            ),
            AttributeInfo(
                name="authors",
                description="List of author names",
                type="list[string]",
            ),
            AttributeInfo(
                name="title",
                description="Title of the paper",
                type="string",
            ),
            AttributeInfo(
                name="categories",
                description="arxiv categories to this paper",
                type="list[string]"
            ),
            AttributeInfo(
                name="length(categories)",
                description="length of arxiv categories to this paper",
                type="int"
            ),
        ],
        "must_have_cols": ['title', 'id', 'categories', 'abstract', 'authors', 'pubdate'],
        "vector_col": "vector",
        "text_col": "abstract",
        "metadata_col": "metadata",
        "emb_model": lambda: HuggingFaceInstructEmbeddings(
            model_name='hkunlp/instructor-xl',
            embed_instruction="Represent the question for retrieving supporting scientific papers: "),
        "tool_desc": ("search_among_scientific_papers", "Searches among scientific papers from ArXiv and returns research papers"),
    }
}


def build_embedding_model(_sel):
    """Build embedding model
    """
    with st.spinner("Loading Model..."):
        embeddings = sel_map[_sel]["emb_model"]()
    return embeddings


def build_chains_retrievers(_sel: str) -> Dict[str, Any]:
    """build chains and retrievers

    :param _sel: selected knowledge base
    :type _sel: str
    :return: _description_
    :rtype: Dict[str, Any]
    """
    metadata_field_info = sel_map[_sel]["metadata_cols"]
    retriever = build_self_query(_sel)
    chain = build_qa_chain(_sel, retriever, name="Self Query Retriever")
    sql_retriever = build_vector_sql(_sel)
    sql_chain = build_qa_chain(_sel, sql_retriever, name="Vector SQL")

    return {
        "metadata_columns": [{'name': m.name.name if type(m.name) is VirtualColumnName else m.name, 'desc': m.description, 'type': m.type} for m in metadata_field_info],
        "retriever": retriever,
        "chain": chain,
        "sql_retriever": sql_retriever,
        "sql_chain": sql_chain
    }


def build_self_query(_sel: str) -> SelfQueryRetriever:
    """Build self querying retriever

    :param _sel: selected knowledge base
    :type _sel: str
    :return: retriever used by chains
    :rtype: SelfQueryRetriever
    """
    with st.spinner(f"Connecting DB for {_sel}..."):
        myscale_connection = {
            "host": MYSCALE_HOST,
            "port": MYSCALE_PORT,
            "username": MYSCALE_USER,
            "password": MYSCALE_PASSWORD,
        }
        config = MyScaleSettings(**myscale_connection,
                                 database=sel_map[_sel]["database"],
                                 table=sel_map[_sel]["table"],
                                 column_map={
                                     "id": "id",
                                     "text": sel_map[_sel]["text_col"],
                                     "vector": sel_map[_sel]["vector_col"],
                                     "metadata": sel_map[_sel]["metadata_col"]
                                 })
        doc_search = MyScaleWithoutMetadataJson(st.session_state[f"emb_model_{_sel}"], config,
                                                must_have_cols=sel_map[_sel]['must_have_cols'])

    with st.spinner(f"Building Self Query Retriever for {_sel}..."):
        metadata_field_info = sel_map[_sel]["metadata_cols"]
        retriever = SelfQueryRetriever.from_llm(
            OpenAI(model_name=query_model_name,
                   openai_api_key=OPENAI_API_KEY, temperature=0),
            doc_search, "Scientific papers indexes with abstracts. All in English.", metadata_field_info,
            use_original_query=False, structured_query_translator=MyScaleTranslator())
    return retriever


def build_vector_sql(_sel: str) -> VectorSQLDatabaseChainRetriever:
    """Build Vector SQL Database Retriever

    :param _sel: selected knowledge base
    :type _sel: str
    :return: retriever used by chains
    :rtype: VectorSQLDatabaseChainRetriever
    """
    with st.spinner(f'Building Vector SQL Database Retriever for {_sel}...'):
        engine = create_engine(
            f'clickhouse://{MYSCALE_USER}:{MYSCALE_PASSWORD}@{MYSCALE_HOST}:{MYSCALE_PORT}/{sel_map[_sel]["database"]}?protocol=https')
        metadata = MetaData(bind=engine)
        PROMPT = PromptTemplate(
            input_variables=["input", "table_info", "top_k"],
            template=_myscale_prompt,
        )
        output_parser = VectorSQLRetrieveCustomOutputParser.from_embeddings(
            model=st.session_state[f'emb_model_{_sel}'], must_have_columns=sel_map[_sel]["must_have_cols"])
        sql_query_chain = VectorSQLDatabaseChain.from_llm(
            llm=OpenAI(model_name=query_model_name,
                       openai_api_key=OPENAI_API_KEY, temperature=0),
            prompt=PROMPT,
            top_k=10,
            return_direct=True,
            db=SQLDatabase(engine, None, metadata, max_string_length=1024),
            sql_cmd_parser=output_parser,
            native_format=True
        )
        sql_retriever = VectorSQLDatabaseChainRetriever(
            sql_db_chain=sql_query_chain, page_content_key=sel_map[_sel]["text_col"])
    return sql_retriever


def build_qa_chain(_sel: str, retriever: BaseRetriever, name: str = "Self-query") -> ArXivQAwithSourcesChain:
    """_summary_

    :param _sel: selected knowledge base
    :type _sel: str
    :param retriever: retriever used by chains
    :type retriever: BaseRetriever
    :param name: display name, defaults to "Self-query"
    :type name: str, optional
    :return: QA chain interacts with user
    :rtype: ArXivQAwithSourcesChain
    """
    with st.spinner(f'Building QA Chain with {name} for {_sel}...'):
        chain = ArXivQAwithSourcesChain(
            retriever=retriever,
            combine_documents_chain=ArXivStuffDocumentChain(
                llm_chain=LLMChain(
                    prompt=COMBINE_PROMPT,
                    llm=ChatOpenAI(model_name=chat_model_name,
                                   openai_api_key=OPENAI_API_KEY, temperature=0.6),
                ),
                document_prompt=sel_map[_sel]["doc_prompt"],
                document_variable_name="summaries",

            ),
            return_source_documents=True,
            max_tokens_limit=12000,
        )
    return chain


@st.cache_resource
def build_all() -> Tuple[Dict[str, Any], Dict[str, Any]]:
    """build all resources

    :return: sel_map_obj
    :rtype: Dict[str, Any]
    """
    sel_map_obj = {}
    embeddings = {}
    for k in sel_map:
        embeddings[k] = build_embedding_model(k)
        st.session_state[f'emb_model_{k}'] = embeddings[k]
        sel_map_obj[k] = build_chains_retrievers(k)
    return sel_map_obj, embeddings


def create_message_model(table_name, DynamicBase):  # type: ignore
    """
    Create a message model for a given table name.

    Args:
        table_name: The name of the table to use.
        DynamicBase: The base class to use for the model.

    Returns:
        The model class.

    """

    # Model decleared inside a function to have a dynamic table name
    class Message(DynamicBase):
        __tablename__ = table_name
        id = Column(types.Float64)
        session_id = Column(Text)
        user_id = Column(Text)
        msg_id = Column(Text, primary_key=True)
        type = Column(Text)
        addtionals = Column(Text)
        message = Column(Text)
        __table_args__ = (
            engines.ReplacingMergeTree(
                partition_by='session_id',
                order_by=('id', 'msg_id')),
            {'comment': 'Store Chat History'}
        )

    return Message


def _message_from_dict(message: dict) -> BaseMessage:
    _type = message["type"]
    if _type == "human":
        return HumanMessage(**message["data"])
    elif _type == "ai":
        return AIMessage(**message["data"])
    elif _type == "system":
        return SystemMessage(**message["data"])
    elif _type == "chat":
        return ChatMessage(**message["data"])
    elif _type == "function":
        return FunctionMessage(**message["data"])
    elif _type == "tool":
        return ToolMessage(**message["data"])
    elif _type == "AIMessageChunk":
        message["data"]["type"] = "ai"
        return AIMessage(**message["data"])
    else:
        raise ValueError(f"Got unexpected message type: {_type}")


class DefaultClickhouseMessageConverter(DefaultMessageConverter):
    """The default message converter for SQLChatMessageHistory."""

    def __init__(self, table_name: str):
        self.model_class = create_message_model(table_name, declarative_base())

    def to_sql_model(self, message: BaseMessage, session_id: str) -> Any:
        tstamp = time.time()
        msg_id = hashlib.sha256(
            f"{session_id}_{message}_{tstamp}".encode('utf-8')).hexdigest()
        user_id, _ = session_id.split("?")
        return self.model_class(
            id=tstamp,
            msg_id=msg_id,
            user_id=user_id,
            session_id=session_id,
            type=message.type,
            addtionals=json.dumps(message.additional_kwargs),
            message=json.dumps({
                "type": message.type,
                "additional_kwargs": {"timestamp": tstamp},
                "data": message.dict()})
        )

    def from_sql_model(self, sql_message: Any) -> BaseMessage:
        msg_dump = json.loads(sql_message.message)
        msg = _message_from_dict(msg_dump)
        msg.additional_kwargs = msg_dump["additional_kwargs"]
        return msg

    def get_sql_model_class(self) -> Any:
        return self.model_class


def create_agent_executor(name, session_id, llm, tools, system_prompt, **kwargs):
    name = name.replace(" ", "_")
    conn_str = f'clickhouse://{MYSCALE_USER}:{MYSCALE_PASSWORD}@{MYSCALE_HOST}:{MYSCALE_PORT}'
    chat_memory = SQLChatMessageHistory(
        session_id,
        connection_string=f'{conn_str}/chat?protocol=https',
        custom_message_converter=DefaultClickhouseMessageConverter(name))
    memory = AgentTokenBufferMemory(llm=llm, chat_memory=chat_memory)

    _system_message = SystemMessage(
        content=system_prompt
    )
    prompt = OpenAIFunctionsAgent.create_prompt(
        system_message=_system_message,
        extra_prompt_messages=[MessagesPlaceholder(variable_name="history")],
    )
    agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt)
    return AgentExecutor(
        agent=agent,
        tools=tools,
        memory=memory,
        verbose=True,
        return_intermediate_steps=True,
        **kwargs
    )


class RetrieverInput(BaseModel):
    query: str = Field(description="query to look up in retriever")


def create_retriever_tool(
    retriever: BaseRetriever, name: str, description: str
) -> Tool:
    """Create a tool to do retrieval of documents.

    Args:
        retriever: The retriever to use for the retrieval
        name: The name for the tool. This will be passed to the language model,
            so should be unique and somewhat descriptive.
        description: The description for the tool. This will be passed to the language
            model, so should be descriptive.

    Returns:
        Tool class to pass to an agent
    """
    def wrap(func):
        def wrapped_retrieve(*args, **kwargs):
            docs: List[Document] = func(*args, **kwargs)
            return json.dumps([d.dict() for d in docs], cls=CustomJSONEncoder)
        return wrapped_retrieve

    return Tool(
        name=name,
        description=description,
        func=wrap(retriever.get_relevant_documents),
        coroutine=retriever.aget_relevant_documents,
        args_schema=RetrieverInput,
    )


@st.cache_resource
def build_tools():
    """build all resources

    :return: sel_map_obj
    :rtype: Dict[str, Any]
    """
    sel_map_obj = {}
    for k in sel_map:
        if f'emb_model_{k}' not in st.session_state:
            st.session_state[f'emb_model_{k}'] = build_embedding_model(k)
        if "sel_map_obj" not in st.session_state:
            st.session_state["sel_map_obj"] = {}
        if k not in st.session_state.sel_map_obj:
            st.session_state["sel_map_obj"][k] = {}
        if "langchain_retriever" not in st.session_state.sel_map_obj[k] or "vecsql_retriever" not in st.session_state.sel_map_obj[k]:
            st.session_state.sel_map_obj[k].update(build_chains_retrievers(k))
        sel_map_obj.update({
            f"{k} + Self Querying": create_retriever_tool(st.session_state.sel_map_obj[k]["retriever"], *sel_map[k]["tool_desc"],),
            f"{k} + Vector SQL": create_retriever_tool(st.session_state.sel_map_obj[k]["sql_retriever"], *sel_map[k]["tool_desc"],),
        })
    return sel_map_obj


def build_agents(session_id, tool_names, chat_model_name=chat_model_name, temperature=0.6, system_prompt=DEFAULT_SYSTEM_PROMPT):
    chat_llm = ChatOpenAI(model_name=chat_model_name, temperature=temperature,
                          openai_api_base=OPENAI_API_BASE, openai_api_key=OPENAI_API_KEY, streaming=True,
                          )
    tools = st.session_state.tools if "tools_with_users" not in st.session_state else st.session_state.tools_with_users
    sel_tools = [tools[k] for k in tool_names]
    agent = create_agent_executor(
        "chat_memory",
        session_id,
        chat_llm,
        tools=sel_tools,
        system_prompt=system_prompt
    )
    return agent


def display(dataframe, columns_=None, index=None):
    if len(dataframe) > 0:
        if index:
            dataframe.set_index(index)
        if columns_:
            st.dataframe(dataframe[columns_])
        else:
            st.dataframe(dataframe)
    else:
        st.write("Sorry 😵 we didn't find any articles related to your query.\n\nMaybe the LLM is too naughty that does not follow our instruction... \n\nPlease try again and use verbs that may match the datatype.", unsafe_allow_html=True)

langchain.agents.openai_functions_agent.agent_token_buffer_memory.AgentTokenBufferMemory langchain.pydantic_v1.Field langchain_experimental.retrievers.vector_sql_database.VectorSQLDatabaseChainRetriever langchain.utilities.sql_database.SQLDatabase langchain.schema.messages.ToolMessage langchain.OpenAI langchain.prompts.ChatPromptTemplate.from_strings langchain.agents.openai_functions_agent.base.OpenAIFunctionsAgent langchain.chains.query_constructor.base.AttributeInfo langchain.embeddings.HuggingFaceInstructEmbeddings langchain.schema.messages.FunctionMessage langchain.chat_models.ChatOpenAI langchain.prompts.chat.MessagesPlaceholder langchain.schema.messages.ChatMessage langchain.prompts.prompt.PromptTemplate langchain.agents.AgentExecutor langchain.embeddings.SentenceTransformerEmbeddings langchain.schema.messages.AIMessage langchain.schema.messages.HumanMessage langchain.chains.query_constructor.base.VirtualColumnName langchain.schema.messages.SystemMessage langchain.vectorstores.MyScaleSettings langchain.retrievers.self_query.myscale.MyScaleTranslator
[((3163, 3322), 'langchain.prompts.ChatPromptTemplate.from_strings', 'ChatPromptTemplate.from_strings', ([], {'string_messages': "[(SystemMessagePromptTemplate, combine_prompt_template), (\n    HumanMessagePromptTemplate, '{question}')]"}), "(string_messages=[(\n    SystemMessagePromptTemplate, combine_prompt_template), (\n    HumanMessagePromptTemplate, '{question}')])\n", (3194, 3322), False, 'from langchain.prompts import PromptTemplate, ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate\n'), ((3590, 4394), 'streamlit.info', 'st.info', (['"""We provides you metadata columns below for query. Please choose a natural expression to describe filters on those columns.\n\nFor example: \n\n*If you want to search papers with complex filters*:\n\n- What is a Bayesian network? Please use articles published later than Feb 2018 and with more than 2 categories and whose title like `computer` and must have `cs.CV` in its category.\n\n*If you want to ask questions based on papers in database*:\n\n- What is PageRank?\n- Did Geoffrey Hinton wrote paper about Capsule Neural Networks?\n- Introduce some applications of GANs published around 2019.\n- 请根据 2019 年左右的文章介绍一下 GAN 的应用都有哪些\n- Veuillez présenter les applications du GAN sur la base des articles autour de 2019 ?\n- Is it possible to synthesize room temperature super conductive material?"""'], {}), '(\n    """We provides you metadata columns below for query. Please choose a natural expression to describe filters on those columns.\n\nFor example: \n\n*If you want to search papers with complex filters*:\n\n- What is a Bayesian network? Please use articles published later than Feb 2018 and with more than 2 categories and whose title like `computer` and must have `cs.CV` in its category.\n\n*If you want to ask questions based on papers in database*:\n\n- What is PageRank?\n- Did Geoffrey Hinton wrote paper about Capsule Neural Networks?\n- Introduce some applications of GANs published around 2019.\n- 请根据 2019 年左右的文章介绍一下 GAN 的应用都有哪些\n- Veuillez présenter les applications du GAN sur la base des articles autour de 2019 ?\n- Is it possible to synthesize room temperature super conductive material?"""\n    )\n', (3597, 4394), True, 'import streamlit as st\n'), ((4574, 4710), 'streamlit.info', 'st.info', (['"""You can retrieve papers with button `Query` or ask questions based on retrieved papers with button `Ask`."""'], {'icon': '"""💡"""'}), "(\n    'You can retrieve papers with button `Query` or ask questions based on retrieved papers with button `Ask`.'\n    , icon='💡')\n", (4581, 4710), True, 'import streamlit as st\n'), ((4705, 5231), 'streamlit.markdown', 'st.markdown', (['"""```sql\nCREATE TABLE default.ChatArXiv (\n    `abstract` String, \n    `id` String, \n    `vector` Array(Float32), \n    `metadata` Object(\'JSON\'), \n    `pubdate` DateTime,\n    `title` String,\n    `categories` Array(String),\n    `authors` Array(String), \n    `comment` String,\n    `primary_category` String,\n    VECTOR INDEX vec_idx vector TYPE MSTG(\'fp16_storage=1\', \'metric_type=Cosine\', \'disk_mode=3\'), \n    CONSTRAINT vec_len CHECK length(vector) = 768) \nENGINE = ReplacingMergeTree ORDER BY id\n```"""'], {}), '(\n    """```sql\nCREATE TABLE default.ChatArXiv (\n    `abstract` String, \n    `id` String, \n    `vector` Array(Float32), \n    `metadata` Object(\'JSON\'), \n    `pubdate` DateTime,\n    `title` String,\n    `categories` Array(String),\n    `authors` Array(String), \n    `comment` String,\n    `primary_category` String,\n    VECTOR INDEX vec_idx vector TYPE MSTG(\'fp16_storage=1\', \'metric_type=Cosine\', \'disk_mode=3\'), \n    CONSTRAINT vec_len CHECK length(vector) = 768) \nENGINE = ReplacingMergeTree ORDER BY id\n```"""\n    )\n', (4716, 5231), True, 'import streamlit as st\n'), ((5245, 5514), 'streamlit.info', 'st.info', (['"""We provides you metadata columns below for query. Please choose a natural expression to describe filters on those columns.\n\nFor example: \n\n- Which company did Elon Musk found?\n- What is Iron Gwazi?\n- What is a Ring in mathematics?\n- 苹果的发源地是那里？\n"""'], {}), '(\n    """We provides you metadata columns below for query. Please choose a natural expression to describe filters on those columns.\n\nFor example: \n\n- Which company did Elon Musk found?\n- What is Iron Gwazi?\n- What is a Ring in mathematics?\n- 苹果的发源地是那里？\n"""\n    )\n', (5252, 5514), True, 'import streamlit as st\n'), ((5611, 5747), 'streamlit.info', 'st.info', (['"""You can retrieve papers with button `Query` or ask questions based on retrieved papers with button `Ask`."""'], {'icon': '"""💡"""'}), "(\n    'You can retrieve papers with button `Query` or ask questions based on retrieved papers with button `Ask`.'\n    , icon='💡')\n", (5618, 5747), True, 'import streamlit as st\n'), ((5742, 6195), 'streamlit.markdown', 'st.markdown', (['"""```sql\nCREATE TABLE wiki.Wikipedia (\n    `id` String, \n    `title` String, \n    `text` String, \n    `url` String, \n    `wiki_id` UInt64, \n    `views` Float32, \n    `paragraph_id` UInt64, \n    `langs` UInt32, \n    `emb` Array(Float32), \n    VECTOR INDEX vec_idx emb TYPE MSTG(\'fp16_storage=1\', \'metric_type=Cosine\', \'disk_mode=3\'), \n    CONSTRAINT emb_len CHECK length(emb) = 768) \nENGINE = ReplacingMergeTree ORDER BY id\n```"""'], {}), '(\n    """```sql\nCREATE TABLE wiki.Wikipedia (\n    `id` String, \n    `title` String, \n    `text` String, \n    `url` String, \n    `wiki_id` UInt64, \n    `views` Float32, \n    `paragraph_id` UInt64, \n    `langs` UInt32, \n    `emb` Array(Float32), \n    VECTOR INDEX vec_idx emb TYPE MSTG(\'fp16_storage=1\', \'metric_type=Cosine\', \'disk_mode=3\'), \n    CONSTRAINT emb_len CHECK length(emb) = 768) \nENGINE = ReplacingMergeTree ORDER BY id\n```"""\n    )\n', (5753, 6195), True, 'import streamlit as st\n'), ((18531, 18587), 'langchain.agents.openai_functions_agent.agent_token_buffer_memory.AgentTokenBufferMemory', 'AgentTokenBufferMemory', ([], {'llm': 'llm', 'chat_memory': 'chat_memory'}), '(llm=llm, chat_memory=chat_memory)\n', (18553, 18587), False, 'from langchain.agents.openai_functions_agent.agent_token_buffer_memory import AgentTokenBufferMemory\n'), ((18611, 18647), 'langchain.schema.messages.SystemMessage', 'SystemMessage', ([], {'content': 'system_prompt'}), '(content=system_prompt)\n', (18624, 18647), False, 'from langchain.schema.messages import BaseMessage, HumanMessage, AIMessage, FunctionMessage, SystemMessage, ChatMessage, ToolMessage\n'), ((18847, 18904), 'langchain.agents.openai_functions_agent.base.OpenAIFunctionsAgent', 'OpenAIFunctionsAgent', ([], {'llm': 'llm', 'tools': 'tools', 'prompt': 'prompt'}), '(llm=llm, tools=tools, prompt=prompt)\n', (18867, 18904), False, 'from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent\n'), ((18916, 19030), 'langchain.agents.AgentExecutor', 'AgentExecutor', ([], {'agent': 'agent', 'tools': 'tools', 'memory': 'memory', 'verbose': '(True)', 'return_intermediate_steps': '(True)'}), '(agent=agent, tools=tools, memory=memory, verbose=True,\n    return_intermediate_steps=True, **kwargs)\n', (18929, 19030), False, 'from langchain.agents import AgentExecutor\n'), ((19133, 19183), 'langchain.pydantic_v1.Field', 'Field', ([], {'description': '"""query to look up in retriever"""'}), "(description='query to look up in retriever')\n", (19138, 19183), False, 'from langchain.pydantic_v1 import BaseModel, Field\n'), ((21372, 21523), 'langchain.chat_models.ChatOpenAI', 'ChatOpenAI', ([], {'model_name': 'chat_model_name', 'temperature': 'temperature', 'openai_api_base': 'OPENAI_API_BASE', 'openai_api_key': 'OPENAI_API_KEY', 'streaming': '(True)'}), '(model_name=chat_model_name, temperature=temperature,\n    openai_api_base=OPENAI_API_BASE, openai_api_key=OPENAI_API_KEY,\n    streaming=True)\n', (21382, 21523), False, 'from langchain.chat_models import ChatOpenAI\n'), ((6361, 6562), 'langchain.prompts.prompt.PromptTemplate', 'PromptTemplate', ([], {'input_variables': "['page_content', 'url', 'title', 'ref_id', 'views']", 'template': '"""Title for Doc #{ref_id}: {title}\n\tviews: {views}\n\tcontent: {page_content}\nSOURCE: {url}"""'}), '(input_variables=[\'page_content\', \'url\', \'title\', \'ref_id\',\n    \'views\'], template=\n    """Title for Doc #{ref_id}: {title}\n\tviews: {views}\n\tcontent: {page_content}\nSOURCE: {url}"""\n    )\n', (6375, 6562), False, 'from langchain.prompts.prompt import PromptTemplate\n'), ((7708, 7997), 'langchain.prompts.prompt.PromptTemplate', 'PromptTemplate', ([], {'input_variables': "['page_content', 'id', 'title', 'ref_id', 'authors', 'pubdate', 'categories']", 'template': '"""Title for Doc #{ref_id}: {title}\n\tAbstract: {page_content}\n\tAuthors: {authors}\n\tDate of Publication: {pubdate}\n\tCategories: {categories}\nSOURCE: {id}"""'}), '(input_variables=[\'page_content\', \'id\', \'title\', \'ref_id\',\n    \'authors\', \'pubdate\', \'categories\'], template=\n    """Title for Doc #{ref_id}: {title}\n\tAbstract: {page_content}\n\tAuthors: {authors}\n\tDate of Publication: {pubdate}\n\tCategories: {categories}\nSOURCE: {id}"""\n    )\n', (7722, 7997), False, 'from langchain.prompts.prompt import PromptTemplate\n'), ((9574, 9604), 'streamlit.spinner', 'st.spinner', (['"""Loading Model..."""'], {}), "('Loading Model...')\n", (9584, 9604), True, 'import streamlit as st\n'), ((10739, 10781), 'streamlit.spinner', 'st.spinner', (['f"""Connecting DB for {_sel}..."""'], {}), "(f'Connecting DB for {_sel}...')\n", (10749, 10781), True, 'import streamlit as st\n'), ((10989, 11247), 'langchain.vectorstores.MyScaleSettings', 'MyScaleSettings', ([], {'database': "sel_map[_sel]['database']", 'table': "sel_map[_sel]['table']", 'column_map': "{'id': 'id', 'text': sel_map[_sel]['text_col'], 'vector': sel_map[_sel][\n    'vector_col'], 'metadata': sel_map[_sel]['metadata_col']}"}), "(**myscale_connection, database=sel_map[_sel]['database'],\n    table=sel_map[_sel]['table'], column_map={'id': 'id', 'text': sel_map[\n    _sel]['text_col'], 'vector': sel_map[_sel]['vector_col'], 'metadata':\n    sel_map[_sel]['metadata_col']})\n", (11004, 11247), False, 'from langchain.vectorstores import MyScaleSettings\n'), ((11538, 11663), 'chains.arxiv_chains.MyScaleWithoutMetadataJson', 'MyScaleWithoutMetadataJson', (["st.session_state[f'emb_model_{_sel}']", 'config'], {'must_have_cols': "sel_map[_sel]['must_have_cols']"}), "(st.session_state[f'emb_model_{_sel}'], config,\n    must_have_cols=sel_map[_sel]['must_have_cols'])\n", (11564, 11663), False, 'from chains.arxiv_chains import MyScaleWithoutMetadataJson\n'), ((11718, 11776), 'streamlit.spinner', 'st.spinner', (['f"""Building Self Query Retriever for {_sel}..."""'], {}), "(f'Building Self Query Retriever for {_sel}...')\n", (11728, 11776), True, 'import streamlit as st\n'), ((12490, 12557), 'streamlit.spinner', 'st.spinner', (['f"""Building Vector SQL Database Retriever for {_sel}..."""'], {}), "(f'Building Vector SQL Database Retriever for {_sel}...')\n", (12500, 12557), True, 'import streamlit as st\n'), ((12576, 12723), 'sqlalchemy.create_engine', 'create_engine', (['f"""clickhouse://{MYSCALE_USER}:{MYSCALE_PASSWORD}@{MYSCALE_HOST}:{MYSCALE_PORT}/{sel_map[_sel][\'database\']}?protocol=https"""'], {}), '(\n    f"clickhouse://{MYSCALE_USER}:{MYSCALE_PASSWORD}@{MYSCALE_HOST}:{MYSCALE_PORT}/{sel_map[_sel][\'database\']}?protocol=https"\n    )\n', (12589, 12723), False, 'from sqlalchemy import create_engine, MetaData\n'), ((12746, 12767), 'sqlalchemy.MetaData', 'MetaData', ([], {'bind': 'engine'}), '(bind=engine)\n', (12754, 12767), False, 'from sqlalchemy import create_engine, MetaData\n'), ((12785, 12880), 'langchain.prompts.prompt.PromptTemplate', 'PromptTemplate', ([], {'input_variables': "['input', 'table_info', 'top_k']", 'template': '_myscale_prompt'}), "(input_variables=['input', 'table_info', 'top_k'], template=\n    _myscale_prompt)\n", (12799, 12880), False, 'from langchain.prompts.prompt import PromptTemplate\n'), ((12935, 13087), 'chains.arxiv_chains.VectorSQLRetrieveCustomOutputParser.from_embeddings', 'VectorSQLRetrieveCustomOutputParser.from_embeddings', ([], {'model': "st.session_state[f'emb_model_{_sel}']", 'must_have_columns': "sel_map[_sel]['must_have_cols']"}), "(model=st.session_state[\n    f'emb_model_{_sel}'], must_have_columns=sel_map[_sel]['must_have_cols'])\n", (12986, 13087), False, 'from chains.arxiv_chains import VectorSQLRetrieveCustomOutputParser\n'), ((13541, 13650), 'langchain_experimental.retrievers.vector_sql_database.VectorSQLDatabaseChainRetriever', 'VectorSQLDatabaseChainRetriever', ([], {'sql_db_chain': 'sql_query_chain', 'page_content_key': "sel_map[_sel]['text_col']"}), "(sql_db_chain=sql_query_chain,\n    page_content_key=sel_map[_sel]['text_col'])\n", (13572, 13650), False, 'from langchain_experimental.retrievers.vector_sql_database import VectorSQLDatabaseChainRetriever\n'), ((14139, 14197), 'streamlit.spinner', 'st.spinner', (['f"""Building QA Chain with {name} for {_sel}..."""'], {}), "(f'Building QA Chain with {name} for {_sel}...')\n", (14149, 14197), True, 'import streamlit as st\n'), ((15683, 15704), 'sqlalchemy.Column', 'Column', (['types.Float64'], {}), '(types.Float64)\n', (15689, 15704), False, 'from sqlalchemy import Column, Text, create_engine, MetaData\n'), ((15726, 15738), 'sqlalchemy.Column', 'Column', (['Text'], {}), '(Text)\n', (15732, 15738), False, 'from sqlalchemy import Column, Text, create_engine, MetaData\n'), ((15757, 15769), 'sqlalchemy.Column', 'Column', (['Text'], {}), '(Text)\n', (15763, 15769), False, 'from sqlalchemy import Column, Text, create_engine, MetaData\n'), ((15787, 15817), 'sqlalchemy.Column', 'Column', (['Text'], {'primary_key': '(True)'}), '(Text, primary_key=True)\n', (15793, 15817), False, 'from sqlalchemy import Column, Text, create_engine, MetaData\n'), ((15833, 15845), 'sqlalchemy.Column', 'Column', (['Text'], {}), '(Text)\n', (15839, 15845), False, 'from sqlalchemy import Column, Text, create_engine, MetaData\n'), ((15867, 15879), 'sqlalchemy.Column', 'Column', (['Text'], {}), '(Text)\n', (15873, 15879), False, 'from sqlalchemy import Column, Text, create_engine, MetaData\n'), ((15898, 15910), 'sqlalchemy.Column', 'Column', (['Text'], {}), '(Text)\n', (15904, 15910), False, 'from sqlalchemy import Column, Text, create_engine, MetaData\n'), ((16265, 16296), 'langchain.schema.messages.HumanMessage', 'HumanMessage', ([], {}), "(**message['data'])\n", (16277, 16296), False, 'from langchain.schema.messages import BaseMessage, HumanMessage, AIMessage, FunctionMessage, SystemMessage, ChatMessage, ToolMessage\n'), ((17209, 17220), 'time.time', 'time.time', ([], {}), '()\n', (17218, 17220), False, 'import time\n'), ((17878, 17909), 'json.loads', 'json.loads', (['sql_message.message'], {}), '(sql_message.message)\n', (17888, 17909), False, 'import json\n'), ((22189, 22429), 'streamlit.write', 'st.write', (['"""Sorry 😵 we didn\'t find any articles related to your query.\n\nMaybe the LLM is too naughty that does not follow our instruction... \n\nPlease try again and use verbs that may match the datatype."""'], {'unsafe_allow_html': '(True)'}), '(\n    """Sorry 😵 we didn\'t find any articles related to your query.\n\nMaybe the LLM is too naughty that does not follow our instruction... \n\nPlease try again and use verbs that may match the datatype."""\n    , unsafe_allow_html=True)\n', (22197, 22429), True, 'import streamlit as st\n'), ((6644, 6734), 'langchain.chains.query_constructor.base.AttributeInfo', 'AttributeInfo', ([], {'name': '"""title"""', 'description': '"""title of the wikipedia page"""', 'type': '"""string"""'}), "(name='title', description='title of the wikipedia page', type\n    ='string')\n", (6657, 6734), False, 'from langchain.chains.query_constructor.base import AttributeInfo, VirtualColumnName\n'), ((6806, 6896), 'langchain.chains.query_constructor.base.AttributeInfo', 'AttributeInfo', ([], {'name': '"""text"""', 'description': '"""paragraph from this wiki page"""', 'type': '"""string"""'}), "(name='text', description='paragraph from this wiki page',\n    type='string')\n", (6819, 6896), False, 'from langchain.chains.query_constructor.base import AttributeInfo, VirtualColumnName\n'), ((6969, 7041), 'langchain.chains.query_constructor.base.AttributeInfo', 'AttributeInfo', ([], {'name': '"""views"""', 'description': '"""number of views"""', 'type': '"""float"""'}), "(name='views', description='number of views', type='float')\n", (6982, 7041), False, 'from langchain.chains.query_constructor.base import AttributeInfo, VirtualColumnName\n'), ((7305, 7413), 'langchain.embeddings.SentenceTransformerEmbeddings', 'SentenceTransformerEmbeddings', ([], {'model_name': '"""sentence-transformers/paraphrase-multilingual-mpnet-base-v2"""'}), "(model_name=\n    'sentence-transformers/paraphrase-multilingual-mpnet-base-v2')\n", (7334, 7413), False, 'from langchain.embeddings import HuggingFaceInstructEmbeddings, SentenceTransformerEmbeddings\n'), ((8278, 8369), 'langchain.chains.query_constructor.base.AttributeInfo', 'AttributeInfo', ([], {'name': '"""authors"""', 'description': '"""List of author names"""', 'type': '"""list[string]"""'}), "(name='authors', description='List of author names', type=\n    'list[string]')\n", (8291, 8369), False, 'from langchain.chains.query_constructor.base import AttributeInfo, VirtualColumnName\n'), ((8441, 8517), 'langchain.chains.query_constructor.base.AttributeInfo', 'AttributeInfo', ([], {'name': '"""title"""', 'description': '"""Title of the paper"""', 'type': '"""string"""'}), "(name='title', description='Title of the paper', type='string')\n", (8454, 8517), False, 'from langchain.chains.query_constructor.base import AttributeInfo, VirtualColumnName\n'), ((8594, 8698), 'langchain.chains.query_constructor.base.AttributeInfo', 'AttributeInfo', ([], {'name': '"""categories"""', 'description': '"""arxiv categories to this paper"""', 'type': '"""list[string]"""'}), "(name='categories', description=\n    'arxiv categories to this paper', type='list[string]')\n", (8607, 8698), False, 'from langchain.chains.query_constructor.base import AttributeInfo, VirtualColumnName\n'), ((8769, 8882), 'langchain.chains.query_constructor.base.AttributeInfo', 'AttributeInfo', ([], {'name': '"""length(categories)"""', 'description': '"""length of arxiv categories to this paper"""', 'type': '"""int"""'}), "(name='length(categories)', description=\n    'length of arxiv categories to this paper', type='int')\n", (8782, 8882), False, 'from langchain.chains.query_constructor.base import AttributeInfo, VirtualColumnName\n'), ((9172, 9335), 'langchain.embeddings.HuggingFaceInstructEmbeddings', 'HuggingFaceInstructEmbeddings', ([], {'model_name': '"""hkunlp/instructor-xl"""', 'embed_instruction': '"""Represent the question for retrieving supporting scientific papers: """'}), "(model_name='hkunlp/instructor-xl',\n    embed_instruction=\n    'Represent the question for retrieving supporting scientific papers: ')\n", (9201, 9335), False, 'from langchain.embeddings import HuggingFaceInstructEmbeddings, SentenceTransformerEmbeddings\n'), ((11900, 11985), 'langchain.OpenAI', 'OpenAI', ([], {'model_name': 'query_model_name', 'openai_api_key': 'OPENAI_API_KEY', 'temperature': '(0)'}), '(model_name=query_model_name, openai_api_key=OPENAI_API_KEY,\n    temperature=0)\n', (11906, 11985), False, 'from langchain import OpenAI\n'), ((15950, 16035), 'clickhouse_sqlalchemy.engines.ReplacingMergeTree', 'engines.ReplacingMergeTree', ([], {'partition_by': '"""session_id"""', 'order_by': "('id', 'msg_id')"}), "(partition_by='session_id', order_by=('id', 'msg_id')\n    )\n", (15976, 16035), False, 'from clickhouse_sqlalchemy import types, engines\n'), ((16336, 16364), 'langchain.schema.messages.AIMessage', 'AIMessage', ([], {}), "(**message['data'])\n", (16345, 16364), False, 'from langchain.schema.messages import BaseMessage, HumanMessage, AIMessage, FunctionMessage, SystemMessage, ChatMessage, ToolMessage\n'), ((17097, 17115), 'sqlalchemy.ext.declarative.declarative_base', 'declarative_base', ([], {}), '()\n', (17113, 17115), False, 'from sqlalchemy.ext.declarative import declarative_base\n'), ((22087, 22120), 'streamlit.dataframe', 'st.dataframe', (['dataframe[columns_]'], {}), '(dataframe[columns_])\n', (22099, 22120), True, 'import streamlit as st\n'), ((22147, 22170), 'streamlit.dataframe', 'st.dataframe', (['dataframe'], {}), '(dataframe)\n', (22159, 22170), True, 'import streamlit as st\n'), ((12174, 12193), 'langchain.retrievers.self_query.myscale.MyScaleTranslator', 'MyScaleTranslator', ([], {}), '()\n', (12191, 12193), False, 'from langchain.retrievers.self_query.myscale import MyScaleTranslator\n'), ((13171, 13256), 'langchain.OpenAI', 'OpenAI', ([], {'model_name': 'query_model_name', 'openai_api_key': 'OPENAI_API_KEY', 'temperature': '(0)'}), '(model_name=query_model_name, openai_api_key=OPENAI_API_KEY,\n    temperature=0)\n', (13177, 13256), False, 'from langchain import OpenAI\n'), ((13373, 13432), 'langchain.utilities.sql_database.SQLDatabase', 'SQLDatabase', (['engine', 'None', 'metadata'], {'max_string_length': '(1024)'}), '(engine, None, metadata, max_string_length=1024)\n', (13384, 13432), False, 'from langchain.utilities.sql_database import SQLDatabase\n'), ((16408, 16440), 'langchain.schema.messages.SystemMessage', 'SystemMessage', ([], {}), "(**message['data'])\n", (16421, 16440), False, 'from langchain.schema.messages import BaseMessage, HumanMessage, AIMessage, FunctionMessage, SystemMessage, ChatMessage, ToolMessage\n'), ((17574, 17611), 'json.dumps', 'json.dumps', (['message.additional_kwargs'], {}), '(message.additional_kwargs)\n', (17584, 17611), False, 'import json\n'), ((18782, 18826), 'langchain.prompts.chat.MessagesPlaceholder', 'MessagesPlaceholder', ([], {'variable_name': '"""history"""'}), "(variable_name='history')\n", (18801, 18826), False, 'from langchain.prompts.chat import MessagesPlaceholder\n'), ((8119, 8152), 'langchain.chains.query_constructor.base.VirtualColumnName', 'VirtualColumnName', ([], {'name': '"""pubdate"""'}), "(name='pubdate')\n", (8136, 8152), False, 'from langchain.chains.query_constructor.base import AttributeInfo, VirtualColumnName\n'), ((16482, 16512), 'langchain.schema.messages.ChatMessage', 'ChatMessage', ([], {}), "(**message['data'])\n", (16493, 16512), False, 'from langchain.schema.messages import BaseMessage, HumanMessage, AIMessage, FunctionMessage, SystemMessage, ChatMessage, ToolMessage\n'), ((16558, 16592), 'langchain.schema.messages.FunctionMessage', 'FunctionMessage', ([], {}), "(**message['data'])\n", (16573, 16592), False, 'from langchain.schema.messages import BaseMessage, HumanMessage, AIMessage, FunctionMessage, SystemMessage, ChatMessage, ToolMessage\n'), ((16634, 16664), 'langchain.schema.messages.ToolMessage', 'ToolMessage', ([], {}), "(**message['data'])\n", (16645, 16664), False, 'from langchain.schema.messages import BaseMessage, HumanMessage, AIMessage, FunctionMessage, SystemMessage, ChatMessage, ToolMessage\n'), ((14437, 14527), 'langchain.chat_models.ChatOpenAI', 'ChatOpenAI', ([], {'model_name': 'chat_model_name', 'openai_api_key': 'OPENAI_API_KEY', 'temperature': '(0.6)'}), '(model_name=chat_model_name, openai_api_key=OPENAI_API_KEY,\n    temperature=0.6)\n', (14447, 14527), False, 'from langchain.chat_models import ChatOpenAI\n'), ((16755, 16783), 'langchain.schema.messages.AIMessage', 'AIMessage', ([], {}), "(**message['data'])\n", (16764, 16783), False, 'from langchain.schema.messages import BaseMessage, HumanMessage, AIMessage, FunctionMessage, SystemMessage, ChatMessage, ToolMessage\n')]