2023-10-26 05:11:13 +00:00
import os
2023-10-26 01:47:42 +00:00
from operator import itemgetter
2023-10-27 02:44:30 +00:00
from typing import List , Tuple
2023-10-26 01:47:42 +00:00
from langchain . chat_models import ChatOpenAI
from langchain . embeddings import OpenAIEmbeddings
2023-10-27 02:44:30 +00:00
from langchain . prompts import ChatPromptTemplate , MessagesPlaceholder
2023-10-26 01:47:42 +00:00
from langchain . prompts . prompt import PromptTemplate
2023-10-27 02:44:30 +00:00
from langchain . schema import AIMessage , HumanMessage , format_document
docs[patch], templates[patch]: Import from core (#14575)
Update imports to use core for the low-hanging fruit changes. Ran
following
```bash
git grep -l 'langchain.schema.runnable' {docs,templates,cookbook} | xargs sed -i '' 's/langchain\.schema\.runnable/langchain_core.runnables/g'
git grep -l 'langchain.schema.output_parser' {docs,templates,cookbook} | xargs sed -i '' 's/langchain\.schema\.output_parser/langchain_core.output_parsers/g'
git grep -l 'langchain.schema.messages' {docs,templates,cookbook} | xargs sed -i '' 's/langchain\.schema\.messages/langchain_core.messages/g'
git grep -l 'langchain.schema.chat_histry' {docs,templates,cookbook} | xargs sed -i '' 's/langchain\.schema\.chat_history/langchain_core.chat_history/g'
git grep -l 'langchain.schema.prompt_template' {docs,templates,cookbook} | xargs sed -i '' 's/langchain\.schema\.prompt_template/langchain_core.prompts/g'
git grep -l 'from langchain.pydantic_v1' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.pydantic_v1/from langchain_core.pydantic_v1/g'
git grep -l 'from langchain.tools.base' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.tools\.base/from langchain_core.tools/g'
git grep -l 'from langchain.chat_models.base' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.chat_models.base/from langchain_core.language_models.chat_models/g'
git grep -l 'from langchain.llms.base' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.llms\.base\ /from langchain_core.language_models.llms\ /g'
git grep -l 'from langchain.embeddings.base' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.embeddings\.base/from langchain_core.embeddings/g'
git grep -l 'from langchain.vectorstores.base' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.vectorstores\.base/from langchain_core.vectorstores/g'
git grep -l 'from langchain.agents.tools' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.agents\.tools/from langchain_core.tools/g'
git grep -l 'from langchain.schema.output' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.schema\.output\ /from langchain_core.outputs\ /g'
git grep -l 'from langchain.schema.embeddings' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.schema\.embeddings/from langchain_core.embeddings/g'
git grep -l 'from langchain.schema.document' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.schema\.document/from langchain_core.documents/g'
git grep -l 'from langchain.schema.agent' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.schema\.agent/from langchain_core.agents/g'
git grep -l 'from langchain.schema.prompt ' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.schema\.prompt\ /from langchain_core.prompt_values /g'
git grep -l 'from langchain.schema.language_model' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.schema\.language_model/from langchain_core.language_models/g'
```
2023-12-12 00:49:10 +00:00
from langchain . vectorstores import Pinecone
from langchain_core . output_parsers import StrOutputParser
from langchain_core . runnables import (
2023-10-27 02:44:30 +00:00
RunnableBranch ,
RunnableLambda ,
2023-12-01 21:36:40 +00:00
RunnableParallel ,
2023-10-27 02:44:30 +00:00
RunnablePassthrough ,
)
2023-10-30 22:19:32 +00:00
from pydantic import BaseModel , Field
2023-10-26 01:47:42 +00:00
2023-10-26 05:11:13 +00:00
if os . environ . get ( " PINECONE_API_KEY " , None ) is None :
raise Exception ( " Missing `PINECONE_API_KEY` environment variable. " )
if os . environ . get ( " PINECONE_ENVIRONMENT " , None ) is None :
raise Exception ( " Missing `PINECONE_ENVIRONMENT` environment variable. " )
PINECONE_INDEX_NAME = os . environ . get ( " PINECONE_INDEX " , " langchain-test " )
2023-10-26 01:47:42 +00:00
### Ingest code - you may need to run this the first time
2023-10-29 05:13:22 +00:00
# # Load
2023-10-26 01:47:42 +00:00
# from langchain.document_loaders import WebBaseLoader
# loader = WebBaseLoader("https://lilianweng.github.io/posts/2023-06-23-agent/")
# data = loader.load()
# # Split
# from langchain.text_splitter import RecursiveCharacterTextSplitter
# text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0)
# all_splits = text_splitter.split_documents(data)
2023-10-26 05:11:13 +00:00
2023-10-26 01:47:42 +00:00
# # Add to vectorDB
# vectorstore = Pinecone.from_documents(
2023-10-26 05:11:13 +00:00
# documents=all_splits, embedding=OpenAIEmbeddings(), index_name=PINECONE_INDEX_NAME
2023-10-26 01:47:42 +00:00
# )
# retriever = vectorstore.as_retriever()
2023-10-26 05:11:13 +00:00
vectorstore = Pinecone . from_existing_index ( PINECONE_INDEX_NAME , OpenAIEmbeddings ( ) )
2023-10-26 01:47:42 +00:00
retriever = vectorstore . as_retriever ( )
# Condense a chat history and follow-up question into a standalone question
_template = """ Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question, in its original language.
Chat History :
{ chat_history }
Follow Up Input : { question }
2023-10-27 02:44:30 +00:00
Standalone question : """ # noqa: E501
2023-10-26 01:47:42 +00:00
CONDENSE_QUESTION_PROMPT = PromptTemplate . from_template ( _template )
# RAG answer synthesis prompt
template = """ Answer the question based only on the following context:
< context >
{ context }
< / context > """
2023-10-27 02:44:30 +00:00
ANSWER_PROMPT = ChatPromptTemplate . from_messages (
[
( " system " , template ) ,
MessagesPlaceholder ( variable_name = " chat_history " ) ,
( " user " , " {question} " ) ,
]
)
2023-10-26 01:47:42 +00:00
# Conversational Retrieval Chain
DEFAULT_DOCUMENT_PROMPT = PromptTemplate . from_template ( template = " {page_content} " )
2023-10-27 02:44:30 +00:00
def _combine_documents (
docs , document_prompt = DEFAULT_DOCUMENT_PROMPT , document_separator = " \n \n "
) :
2023-10-26 01:47:42 +00:00
doc_strings = [ format_document ( doc , document_prompt ) for doc in docs ]
return document_separator . join ( doc_strings )
2023-10-27 02:44:30 +00:00
2023-10-26 01:47:42 +00:00
def _format_chat_history ( chat_history : List [ Tuple [ str , str ] ] ) - > List :
buffer = [ ]
for human , ai in chat_history :
buffer . append ( HumanMessage ( content = human ) )
buffer . append ( AIMessage ( content = ai ) )
return buffer
2023-10-27 02:44:30 +00:00
2023-10-26 05:11:13 +00:00
# User input
2023-10-26 01:47:42 +00:00
class ChatHistory ( BaseModel ) :
2023-10-30 22:19:32 +00:00
chat_history : List [ Tuple [ str , str ] ] = Field ( . . . , extra = { " widget " : { " type " : " chat " } } )
2023-10-26 01:47:42 +00:00
question : str
_search_query = RunnableBranch (
2023-10-27 02:44:30 +00:00
# If input includes chat_history, we condense it with the follow-up question
(
RunnableLambda ( lambda x : bool ( x . get ( " chat_history " ) ) ) . with_config (
run_name = " HasChatHistoryCheck "
) , # Condense follow-up question and chat into a standalone_question
RunnablePassthrough . assign (
chat_history = lambda x : _format_chat_history ( x [ " chat_history " ] )
)
| CONDENSE_QUESTION_PROMPT
| ChatOpenAI ( temperature = 0 )
| StrOutputParser ( ) ,
) ,
# Else, we have no chat history, so just pass through the question
RunnableLambda ( itemgetter ( " question " ) ) ,
)
2023-12-01 21:36:40 +00:00
_inputs = RunnableParallel (
2023-10-27 02:44:30 +00:00
{
" question " : lambda x : x [ " question " ] ,
" chat_history " : lambda x : _format_chat_history ( x [ " chat_history " ] ) ,
" context " : _search_query | retriever | _combine_documents ,
}
) . with_types ( input_type = ChatHistory )
2023-10-26 01:47:42 +00:00
chain = _inputs | ANSWER_PROMPT | ChatOpenAI ( ) | StrOutputParser ( )