Skip to content

Commit

Permalink
Fix RAG prompt to pass top hit as context (elastic#39)
Browse files Browse the repository at this point in the history
* Fix RAG prompt to pass top hit as context

* Update question

* Multiline for GH preview
  • Loading branch information
leemthompo authored Aug 11, 2023
1 parent eaf6e8f commit a026d0f
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions notebooks/integrations/openai/openai-KNN-RAG.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@
"EMBEDDING_MODEL = \"text-embedding-ada-002\"\n",
"\n",
"# Define question\n",
"question = 'How wide is the Atlantic ocean?'\n",
"question = 'How big is the Atlantic ocean?'\n",
"\n",
"# Create embedding\n",
"question_embedding = openai.Embedding.create(input=question, model=EMBEDDING_MODEL)\n"
Expand Down Expand Up @@ -353,16 +353,15 @@
" \"num_candidates\": 100\n",
" }\n",
")\n",
"pretty_response(response)"
"pretty_response(response)\n",
"top_hit_summary = response['hits']['hits'][0]['_source']['text'] # Store content of top hit for final step"
]
},
{
"cell_type": "markdown",
"id": "276c1147",
"metadata": {},
"source": [
"## Next steps\n",
"\n",
"Success! Now you know how to use Elasticsearch as a vector database to store embeddings, encode queries by calling the OpenAI [`embeddings`](https://platform.openai.com/docs/api-reference/embeddings) endpoint, and run semantic search using [kNN search](https://www.elastic.co/guide/en/elasticsearch/reference/current/knn-search.html) to find the top results.\n",
"\n",
"Play around with different queries, and if you want to try with your own data, you can experiment with different embedding models.\n",
Expand Down Expand Up @@ -395,7 +394,10 @@
" model=\"gpt-3.5-turbo\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
" {\"role\": \"user\", \"content\": \"Answer the following question:\" + question + \"by using the following text:\"},\n",
" {\"role\": \"user\", \"content\": \"Answer the following question:\" \n",
" + question \n",
" + \"by using the following text:\" \n",
" + top_hit_summary},\n",
" ]\n",
")\n",
"\n",
Expand Down

0 comments on commit a026d0f

Please sign in to comment.