Slide 25
Slide 25 text
OpenSearchの検索API
def search(query: str):
response = client.search(
index=index_name,
body={
"_source": {"exclude": ["body_chunk_embedding"]},
"query": {
"hybrid": {
"queries": [
{"match": {"body_chunk": {"query": query,}}},
{"nested": {
"score_mode": "max",
"path": "body_chunk_embedding",
"query": {
"neural": {
"body_chunk_embedding.knn": {
"query_text": query,
"model_id": titan_model_id,
}}},}},],}},
"ext": {
"rerank": {"query_context": {"query_text": query,},},
"generative_qa_parameters": {
"llm_model": "litellm",
"llm_question": query,
"context_size": 4,
},},},
params={"search_pipeline": "hybrid-rerank-search-pipeline"},
)
25
context = list(map(lambda x: x["_source"], response["hits"]["hits"]))
for tmp in context:
del tmp["body_chunk"]
return {
"answer": response["ext"]["retrieval_augmented_generation"]["answer"],
"context": context,
}
検索パイプラインを定義しておくことで、検索
APIを呼び出すだけでRAGの結果を取得できる