|
| 1 | +from typing import Any, Awaitable, List, Optional, Union, cast |
| 2 | + |
| 3 | +from azure.search.documents.aio import SearchClient |
| 4 | +from azure.search.documents.models import VectorQuery |
| 5 | +from openai import AsyncOpenAI, AsyncStream |
| 6 | +from openai.types.chat import ( |
| 7 | + ChatCompletion, |
| 8 | + ChatCompletionChunk, |
| 9 | + ChatCompletionMessageParam, |
| 10 | + ChatCompletionToolParam, |
| 11 | +) |
| 12 | + |
| 13 | +from approaches.approach import DataPoints, ExtraInfo, ThoughtStep |
| 14 | +from approaches.chatapproach import ChatApproach |
| 15 | +from approaches.promptmanager import PromptManager |
| 16 | +from core.authentication import AuthenticationHelper |
| 17 | + |
| 18 | + |
| 19 | +class ChatReadRetrieveReadApproach(ChatApproach): |
| 20 | + """ |
| 21 | + A multi-step approach that first uses OpenAI to turn the user's question into a search query, |
| 22 | + then uses Azure AI Search to retrieve relevant documents, and then sends the conversation history, |
| 23 | + original user question, and search results to OpenAI to generate a response. |
| 24 | + """ |
| 25 | + |
| 26 | + def __init__( |
| 27 | + self, |
| 28 | + *, |
| 29 | + search_client: SearchClient, |
| 30 | + auth_helper: AuthenticationHelper, |
| 31 | + openai_client: AsyncOpenAI, |
| 32 | + chatgpt_model: str, |
| 33 | + chatgpt_deployment: Optional[str], # Not needed for non-Azure OpenAI |
| 34 | + embedding_deployment: Optional[str], # Not needed for non-Azure OpenAI or for retrieval_mode="text" |
| 35 | + embedding_model: str, |
| 36 | + embedding_dimensions: int, |
| 37 | + sourcepage_field: str, |
| 38 | + content_field: str, |
| 39 | + query_language: str, |
| 40 | + query_speller: str, |
| 41 | + prompt_manager: PromptManager, |
| 42 | + reasoning_effort: Optional[str] = None, |
| 43 | + ): |
| 44 | + self.search_client = search_client |
| 45 | + self.openai_client = openai_client |
| 46 | + self.auth_helper = auth_helper |
| 47 | + self.chatgpt_model = chatgpt_model |
| 48 | + self.chatgpt_deployment = chatgpt_deployment |
| 49 | + self.embedding_deployment = embedding_deployment |
| 50 | + self.embedding_model = embedding_model |
| 51 | + self.embedding_dimensions = embedding_dimensions |
| 52 | + self.sourcepage_field = sourcepage_field |
| 53 | + self.content_field = content_field |
| 54 | + self.query_language = query_language |
| 55 | + self.query_speller = query_speller |
| 56 | + self.prompt_manager = prompt_manager |
| 57 | + self.query_rewrite_prompt = self.prompt_manager.load_prompt("chat_query_rewrite.prompty") |
| 58 | + self.query_rewrite_tools = self.prompt_manager.load_tools("chat_query_rewrite_tools.json") |
| 59 | + self.answer_prompt = self.prompt_manager.load_prompt("chat_answer_question.prompty") |
| 60 | + self.reasoning_effort = reasoning_effort |
| 61 | + self.include_token_usage = True |
| 62 | + |
| 63 | + async def run_until_final_call( |
| 64 | + self, |
| 65 | + messages: list[ChatCompletionMessageParam], |
| 66 | + overrides: dict[str, Any], |
| 67 | + auth_claims: dict[str, Any], |
| 68 | + should_stream: bool = False, |
| 69 | + ) -> tuple[ExtraInfo, Union[Awaitable[ChatCompletion], Awaitable[AsyncStream[ChatCompletionChunk]]]]: |
| 70 | + use_text_search = overrides.get("retrieval_mode") in ["text", "hybrid", None] |
| 71 | + use_vector_search = overrides.get("retrieval_mode") in ["vectors", "hybrid", None] |
| 72 | + use_semantic_ranker = True if overrides.get("semantic_ranker") else False |
| 73 | + use_semantic_captions = True if overrides.get("semantic_captions") else False |
| 74 | + use_query_rewriting = True if overrides.get("query_rewriting") else False |
| 75 | + top = overrides.get("top", 3) |
| 76 | + minimum_search_score = overrides.get("minimum_search_score", 0.0) |
| 77 | + minimum_reranker_score = overrides.get("minimum_reranker_score", 0.0) |
| 78 | + filter = self.build_filter(overrides, auth_claims) |
| 79 | + |
| 80 | + original_user_query = messages[-1]["content"] |
| 81 | + if not isinstance(original_user_query, str): |
| 82 | + raise ValueError("The most recent message content must be a string.") |
| 83 | + |
| 84 | + reasoning_model_support = self.GPT_REASONING_MODELS.get(self.chatgpt_model) |
| 85 | + if reasoning_model_support and (not reasoning_model_support.streaming and should_stream): |
| 86 | + raise Exception( |
| 87 | + f"{self.chatgpt_model} does not support streaming. Please use a different model or disable streaming." |
| 88 | + ) |
| 89 | + |
| 90 | + query_messages = self.prompt_manager.render_prompt( |
| 91 | + self.query_rewrite_prompt, {"user_query": original_user_query, "past_messages": messages[:-1]} |
| 92 | + ) |
| 93 | + tools: List[ChatCompletionToolParam] = self.query_rewrite_tools |
| 94 | + |
| 95 | + # STEP 1: Generate an optimized keyword search query based on the chat history and the last question |
| 96 | + |
| 97 | + chat_completion = cast( |
| 98 | + ChatCompletion, |
| 99 | + await self.create_chat_completion( |
| 100 | + self.chatgpt_deployment, |
| 101 | + self.chatgpt_model, |
| 102 | + messages=query_messages, |
| 103 | + overrides=overrides, |
| 104 | + response_token_limit=self.get_response_token_limit( |
| 105 | + self.chatgpt_model, 100 |
| 106 | + ), # Setting too low risks malformed JSON, setting too high may affect performance |
| 107 | + temperature=0.0, # Minimize creativity for search query generation |
| 108 | + tools=tools, |
| 109 | + reasoning_effort="low", # Minimize reasoning for search query generation |
| 110 | + ), |
| 111 | + ) |
| 112 | + |
| 113 | + query_text = self.get_search_query(chat_completion, original_user_query) |
| 114 | + |
| 115 | + # STEP 2: Retrieve relevant documents from the search index with the GPT optimized query |
| 116 | + |
| 117 | + # If retrieval mode includes vectors, compute an embedding for the query |
| 118 | + vectors: list[VectorQuery] = [] |
| 119 | + if use_vector_search: |
| 120 | + vectors.append(await self.compute_text_embedding(query_text)) |
| 121 | + |
| 122 | + results = await self.search( |
| 123 | + top, |
| 124 | + query_text, |
| 125 | + filter, |
| 126 | + vectors, |
| 127 | + use_text_search, |
| 128 | + use_vector_search, |
| 129 | + use_semantic_ranker, |
| 130 | + use_semantic_captions, |
| 131 | + minimum_search_score, |
| 132 | + minimum_reranker_score, |
| 133 | + use_query_rewriting, |
| 134 | + ) |
| 135 | + |
| 136 | + # STEP 3: Generate a contextual and content specific answer using the search results and chat history |
| 137 | + text_sources = self.get_sources_content(results, use_semantic_captions, use_image_citation=False) |
| 138 | + messages = self.prompt_manager.render_prompt( |
| 139 | + self.answer_prompt, |
| 140 | + self.get_system_prompt_variables(overrides.get("prompt_template")) |
| 141 | + | { |
| 142 | + "include_follow_up_questions": bool(overrides.get("suggest_followup_questions")), |
| 143 | + "past_messages": messages[:-1], |
| 144 | + "user_query": original_user_query, |
| 145 | + "text_sources": text_sources, |
| 146 | + }, |
| 147 | + ) |
| 148 | + |
| 149 | + extra_info = ExtraInfo( |
| 150 | + DataPoints(text=text_sources), |
| 151 | + thoughts=[ |
| 152 | + self.format_thought_step_for_chatcompletion( |
| 153 | + title="Prompt to generate search query", |
| 154 | + messages=query_messages, |
| 155 | + overrides=overrides, |
| 156 | + model=self.chatgpt_model, |
| 157 | + deployment=self.chatgpt_deployment, |
| 158 | + usage=chat_completion.usage, |
| 159 | + reasoning_effort="low", |
| 160 | + ), |
| 161 | + ThoughtStep( |
| 162 | + "Search using generated search query", |
| 163 | + query_text, |
| 164 | + { |
| 165 | + "use_semantic_captions": use_semantic_captions, |
| 166 | + "use_semantic_ranker": use_semantic_ranker, |
| 167 | + "use_query_rewriting": use_query_rewriting, |
| 168 | + "top": top, |
| 169 | + "filter": filter, |
| 170 | + "use_vector_search": use_vector_search, |
| 171 | + "use_text_search": use_text_search, |
| 172 | + }, |
| 173 | + ), |
| 174 | + ThoughtStep( |
| 175 | + "Search results", |
| 176 | + [result.serialize_for_results() for result in results], |
| 177 | + ), |
| 178 | + self.format_thought_step_for_chatcompletion( |
| 179 | + title="Prompt to generate answer", |
| 180 | + messages=messages, |
| 181 | + overrides=overrides, |
| 182 | + model=self.chatgpt_model, |
| 183 | + deployment=self.chatgpt_deployment, |
| 184 | + usage=None, |
| 185 | + ), |
| 186 | + ], |
| 187 | + ) |
| 188 | + |
| 189 | + chat_coroutine = cast( |
| 190 | + Union[Awaitable[ChatCompletion], Awaitable[AsyncStream[ChatCompletionChunk]]], |
| 191 | + self.create_chat_completion( |
| 192 | + self.chatgpt_deployment, |
| 193 | + self.chatgpt_model, |
| 194 | + messages, |
| 195 | + overrides, |
| 196 | + self.get_response_token_limit(self.chatgpt_model, 1024), |
| 197 | + should_stream, |
| 198 | + ), |
| 199 | + ) |
| 200 | + return (extra_info, chat_coroutine) |
0 commit comments