Skip to content

Commit 5610593

Browse files
author
Stainless Bot
committed
feat: chore: show how to log context in RAG notebook example
1 parent 0e7f228 commit 5610593

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

examples/tracing/rag/rag_tracing.ipynb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"outputs": [],
2020
"source": [
2121
"import os\n",
22-
"import openai\n",
2322
"\n",
2423
"# OpenAI env variables\n",
2524
"os.environ[\"OPENAI_API_KEY\"] = \"YOUR_OPENAI_API_KEY_HERE\"\n",
@@ -58,13 +57,12 @@
5857
"metadata": {},
5958
"outputs": [],
6059
"source": [
61-
"import random\n",
62-
"import time\n",
60+
"from typing import List\n",
6361
"\n",
6462
"import numpy as np\n",
6563
"from openai import OpenAI\n",
66-
"from sklearn.feature_extraction.text import TfidfVectorizer\n",
6764
"from sklearn.metrics.pairwise import cosine_similarity\n",
65+
"from sklearn.feature_extraction.text import TfidfVectorizer\n",
6866
"\n",
6967
"from openlayer.lib import trace, trace_openai"
7068
]
@@ -93,31 +91,35 @@
9391
"\n",
9492
" Answers to a user query with the LLM.\n",
9593
" \"\"\"\n",
96-
" context = self.retrieve_context(user_query)\n",
94+
" context = self.retrieve_contexts(user_query)\n",
9795
" prompt = self.inject_prompt(user_query, context)\n",
9896
" answer = self.generate_answer_with_gpt(prompt)\n",
9997
" return answer\n",
10098
"\n",
10199
" @trace()\n",
102-
" def retrieve_context(self, query: str) -> str:\n",
100+
" def retrieve_contexts(self, query: str) -> List[str]:\n",
103101
" \"\"\"Context retriever.\n",
104102
"\n",
105103
" Given the query, returns the most similar context (using TFIDF).\n",
106104
" \"\"\"\n",
107105
" query_vector = self.vectorizer.transform([query])\n",
108106
" cosine_similarities = cosine_similarity(query_vector, self.tfidf_matrix).flatten()\n",
109107
" most_relevant_idx = np.argmax(cosine_similarities)\n",
110-
" return self.context_sections[most_relevant_idx]\n",
108+
" contexts = [self.context_sections[most_relevant_idx]]\n",
109+
" return contexts\n",
111110
"\n",
112-
" @trace()\n",
113-
" def inject_prompt(self, query: str, context: str):\n",
111+
" # You can also specify the name of the `context_kwarg` to unlock RAG metrics that\n",
112+
" # evaluate the performance of the context retriever. The value of the `context_kwarg`\n",
113+
" # should be a list of strings.\n",
114+
" @trace(context_kwarg=\"contexts\")\n",
115+
" def inject_prompt(self, query: str, contexts: List[str]) -> List[dict]:\n",
114116
" \"\"\"Combines the query with the context and returns\n",
115117
" the prompt (formatted to conform with OpenAI models).\"\"\"\n",
116118
" return [\n",
117119
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
118120
" {\n",
119121
" \"role\": \"user\",\n",
120-
" \"content\": f\"Answer the user query using only the following context: {context}. \\nUser query: {query}\",\n",
122+
" \"content\": f\"Answer the user query using only the following context: {contexts[0]}. \\nUser query: {query}\",\n",
121123
" },\n",
122124
" ]\n",
123125
"\n",
@@ -172,7 +174,7 @@
172174
{
173175
"cell_type": "code",
174176
"execution_count": null,
175-
"id": "f960a36f-3438-4c81-8cdb-ca078aa509cd",
177+
"id": "a45d5562",
176178
"metadata": {},
177179
"outputs": [],
178180
"source": []

0 commit comments

Comments
 (0)