import ( ChatPromptTemplate, HumanMessagePromptTemplate, ) from langchain import LLMChain from langchain import SQLDatabase, SQLDatabaseChain sql_url = "sqlite:///sqlite-sakila-sample-database/sqlite-sakila.db" tables = ["customer", "store", "staff"] sql_database = SQLDatabase.from_uri( sql_url, include_tables=tables) llm = ChatOpenAI(temperature=0) template = ¥ """ Given an input question. Question: {input} First create a syntactically correct {dialect} query to run, then look at the results of the query and return the answer. Only use the following tables: {table_info} """ prompt = ChatPromptTemplate.from_messages( [HumanMessagePromptTemplate.from_template(template)] ) llm_chain = LLMChain(llm=llm, prompt=prompt) db_chain = SQLDatabaseChain( llm_chain=llm_chain, database=sql_database, return_intermediate_steps=True) result = db_chain("姓が「MA」で始まる顧客情報を3名分取得し、その名前を回答してください。") print(result["result"])
[1:chain:RetrievalQA] Entering Chain run with input: { "query": "re:Inventではどのようなアップデートがありましたか?" } [chain/start] [1:chain:RetrievalQA > 3:chain:StuffDocumentsChain] Entering Chain run with input: [inputs] [chain/start] [1:chain:RetrievalQA > 3:chain:StuffDocumentsChain > 4:chain:LLMChain] Entering Chain run with input: { "question": "re:Inventではどのようなアップデートがありましたか?", "context": "¥"はい、みなさんこんばんは。 クラスメソッドがですね、ラスベガスからリインベントの様子をお届けしま す。 ... } [llm/start] [1:chain:RetrievalQA > 3:chain:StuffDocumentsChain > 4:chain:LLMChain > 5:llm:ChatOpenAI] Entering LLM run with input: { "prompts": [ "System: Use the following pieces of context to answer the users question. ¥nIf you don't know the answer, just say that you don't know, don't try to make up an answer.¥n----------------¥n¥"はい、みなさんこんばんは。 ] } [llm/end] [1:chain:RetrievalQA > 3:chain:StuffDocumentsChain > 4:chain:LLMChain > 5:llm:ChatOpenAI] [1.15s] Exiting LLM run with output: { "generations": [ [ { ...
Document Store Vector Store Index Store Service Context Node Parser Embeddings LLMPredictor PromptHelper CallbackManager LlamaLogger Index Retriever Response Synthesizer RetrieverQueryEngine PandasQueryEngine SimpleChatEngine NLSQLTableQueryEngine ListIndex VectorIndex TreeIndex
ListRetrieverMode.EMBEDDING 埋め込みベクトルを使って抽出 Vector Index 一意 埋め込みベクトルを使って抽出 Tree Index TreeRetrieverMode.SELECT_LEAF プロンプトを使ってLeafノードを探索して抽出 Tree Index TreeRetrieverMode.SELECT_LEAF_EMBEDDING 埋め込みベクトルを使ってLeafノードを探索して抽出 Tree Index TreeRetrieverMode.ALL_LEAF 全てのLeafノードを使いクエリ固有のツリーを構築して応答 Tree Index TreeRetrieverMode.ROOT ルートノードのみを使って応答 Table Index KeywordTableRetrieverMode.DEFAULT GPTを使ってクエリのキーワード抽出を行う Table Index KeywordTableRetrieverMode.SIMPLE 正規表現を使ってクエリのキーワード抽出を行う Table Index KeywordTableRetrieverMode.RAKE RAKEキーワード抽出器を使ってクエリのキーワード抽出を行 う