|
| 1 | +import logging |
| 2 | + |
| 3 | +logger = logging.getLogger(__name__) |
| 4 | + |
| 5 | +from hamilton import contrib |
| 6 | + |
| 7 | +with contrib.catch_import_errors(__name__, __file__, logger): |
| 8 | + import openai |
| 9 | + |
| 10 | + # use langchain implementation of vector store |
| 11 | + from langchain_community.vectorstores import FAISS |
| 12 | + from langchain_core.vectorstores import VectorStoreRetriever |
| 13 | + |
| 14 | + # use langchain embedding wrapper with vector store |
| 15 | + from langchain_openai import OpenAIEmbeddings |
| 16 | + |
| 17 | + |
| 18 | +def standalone_question_prompt(chat_history: list[str], question: str) -> str: |
| 19 | + """Prompt for getting a standalone question given the chat history. |
| 20 | +
|
| 21 | + This is then used to query the vector store with. |
| 22 | +
|
| 23 | + :param chat_history: the history of the conversation. |
| 24 | + :param question: the current user question. |
| 25 | + :return: prompt to use. |
| 26 | + """ |
| 27 | + chat_history_str = "\n".join(chat_history) |
| 28 | + return ( |
| 29 | + "Given the following conversation and a follow up question, " |
| 30 | + "rephrase the follow up question to be a standalone question, " |
| 31 | + "in its original language.\n\n" |
| 32 | + "Chat History:\n" |
| 33 | + "{chat_history}\n" |
| 34 | + "Follow Up Input: {question}\n" |
| 35 | + "Standalone question:" |
| 36 | + ).format(chat_history=chat_history_str, question=question) |
| 37 | + |
| 38 | + |
| 39 | +def standalone_question(standalone_question_prompt: str, llm_client: openai.OpenAI) -> str: |
| 40 | + """Asks the LLM to create a standalone question from the prompt. |
| 41 | +
|
| 42 | + :param standalone_question_prompt: the prompt with context. |
| 43 | + :param llm_client: the llm client to use. |
| 44 | + :return: the standalone question. |
| 45 | + """ |
| 46 | + response = llm_client.chat.completions.create( |
| 47 | + model="gpt-3.5-turbo", |
| 48 | + messages=[{"role": "user", "content": standalone_question_prompt}], |
| 49 | + ) |
| 50 | + return response.choices[0].message.content |
| 51 | + |
| 52 | + |
| 53 | +def vector_store(input_texts: list[str]) -> VectorStoreRetriever: |
| 54 | + """A Vector store. This function populates and creates one for querying. |
| 55 | +
|
| 56 | + This is a cute function encapsulating the creation of a vector store. In real life |
| 57 | + you could replace this with a more complex function, or one that returns a |
| 58 | + client to an existing vector store. |
| 59 | +
|
| 60 | + :param input_texts: the input "text" i.e. documents to be stored. |
| 61 | + :return: a vector store that can be queried against. |
| 62 | + """ |
| 63 | + vectorstore = FAISS.from_texts(input_texts, embedding=OpenAIEmbeddings()) |
| 64 | + retriever = vectorstore.as_retriever() |
| 65 | + return retriever |
| 66 | + |
| 67 | + |
| 68 | +def context(standalone_question: str, vector_store: VectorStoreRetriever, top_k: int = 5) -> str: |
| 69 | + """This function returns the string context to put into a prompt for the RAG model. |
| 70 | +
|
| 71 | + It queries the provided vector store for information. |
| 72 | +
|
| 73 | + :param standalone_question: the question to use to search the vector store against. |
| 74 | + :param vector_store: the vector store to search against. |
| 75 | + :param top_k: the number of results to return. |
| 76 | + :return: a string with all the context. |
| 77 | + """ |
| 78 | + _results = vector_store.invoke(standalone_question, search_kwargs={"k": top_k}) |
| 79 | + return "\n\n".join(map(lambda d: d.page_content, _results)) |
| 80 | + |
| 81 | + |
| 82 | +def answer_prompt(context: str, standalone_question: str) -> str: |
| 83 | + """Creates a prompt that includes the question and context for the LLM to make sense of. |
| 84 | +
|
| 85 | + :param context: the information context to use. |
| 86 | + :param standalone_question: the user question the LLM should answer. |
| 87 | + :return: the full prompt. |
| 88 | + """ |
| 89 | + template = ( |
| 90 | + "Answer the question based only on the following context:\n" |
| 91 | + "{context}\n\n" |
| 92 | + "Question: {question}" |
| 93 | + ) |
| 94 | + |
| 95 | + return template.format(context=context, question=standalone_question) |
| 96 | + |
| 97 | + |
| 98 | +def llm_client() -> openai.OpenAI: |
| 99 | + """The LLM client to use for the RAG model.""" |
| 100 | + return openai.OpenAI() |
| 101 | + |
| 102 | + |
| 103 | +def conversational_rag_response(answer_prompt: str, llm_client: openai.OpenAI) -> str: |
| 104 | + """Creates the RAG response from the LLM model for the given prompt. |
| 105 | +
|
| 106 | + :param answer_prompt: the prompt to send to the LLM. |
| 107 | + :param llm_client: the LLM client to use. |
| 108 | + :return: the response from the LLM. |
| 109 | + """ |
| 110 | + response = llm_client.chat.completions.create( |
| 111 | + model="gpt-3.5-turbo", |
| 112 | + messages=[{"role": "user", "content": answer_prompt}], |
| 113 | + ) |
| 114 | + return response.choices[0].message.content |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + import __init__ as conversational_rag |
| 119 | + |
| 120 | + from hamilton import driver, lifecycle |
| 121 | + |
| 122 | + dr = ( |
| 123 | + driver.Builder() |
| 124 | + .with_modules(conversational_rag) |
| 125 | + .with_config({}) |
| 126 | + # this prints the inputs and outputs of each step. |
| 127 | + .with_adapters(lifecycle.PrintLn(verbosity=2)) |
| 128 | + .build() |
| 129 | + ) |
| 130 | + dr.display_all_functions("dag.png") |
| 131 | + |
| 132 | + # shows no question is reworded |
| 133 | + print( |
| 134 | + dr.execute( |
| 135 | + ["conversational_rag_response"], |
| 136 | + inputs={ |
| 137 | + "input_texts": [ |
| 138 | + "harrison worked at kensho", |
| 139 | + "stefan worked at Stitch Fix", |
| 140 | + ], |
| 141 | + "question": "where did stefan work?", |
| 142 | + "chat_history": [], |
| 143 | + }, |
| 144 | + ) |
| 145 | + ) |
| 146 | + |
| 147 | + # this will now reword the question to then be |
| 148 | + # used to query the vector store. |
| 149 | + print( |
| 150 | + dr.execute( |
| 151 | + ["conversational_rag_response"], |
| 152 | + inputs={ |
| 153 | + "input_texts": [ |
| 154 | + "harrison worked at kensho", |
| 155 | + "stefan worked at Stitch Fix", |
| 156 | + ], |
| 157 | + "question": "where did he work?", |
| 158 | + "chat_history": ["Human: Who wrote this example?", "AI: Stefan"], |
| 159 | + }, |
| 160 | + ) |
| 161 | + ) |
0 commit comments