Delete 'task5_model_evaluation/Model_Evaluation.ipynb'
Browse files
task5_model_evaluation/Model_Evaluation.ipynb
DELETED
@@ -1,571 +0,0 @@
|
|
1 |
-
{
|
2 |
-
"cells": [
|
3 |
-
{
|
4 |
-
"cell_type": "markdown",
|
5 |
-
"metadata": {},
|
6 |
-
"source": [
|
7 |
-
"Model Evaluation using TruLens\n",
|
8 |
-
"\n",
|
9 |
-
"TruLens is a powerful tool for evaluating machine learning models. It provides a comprehensive set of metrics and visualizations to assess the performance and effectiveness of your models. Here are some key steps to perform model evaluation using TruLens:\n",
|
10 |
-
"\n",
|
11 |
-
"1. **Import the necessary libraries**: Begin by importing the required libraries, including TruLens, pandas, and numpy.\n",
|
12 |
-
"\n",
|
13 |
-
"2. **Load the dataset**: Load the dataset that you want to evaluate your model on. Ensure that the dataset is properly preprocessed and split into training and testing sets.\n",
|
14 |
-
"\n",
|
15 |
-
"3. **Train your model**: Train your machine learning model using the training dataset. This step involves fitting the model to the training data and optimizing its parameters.\n",
|
16 |
-
"\n",
|
17 |
-
"4. **Make predictions**: Use the trained model to make predictions on the testing dataset. This step involves passing the testing data through the model and obtaining the predicted outputs.\n",
|
18 |
-
"\n",
|
19 |
-
"5. **Evaluate the model**: Now it's time to evaluate the performance of your model using TruLens. TruLens provides various evaluation metrics such as accuracy, precision, recall, F1 score, and ROC curve. These metrics help you understand how well your model is performing and identify areas for improvement.\n",
|
20 |
-
"\n",
|
21 |
-
"6. **Visualize the results**: TruLens also offers powerful visualization capabilities to help you gain insights into your model's performance. You can generate visualizations such as confusion matrices, precision-recall curves, and ROC curves to better understand the behavior of your model.\n",
|
22 |
-
"\n",
|
23 |
-
"7. **Iterate and improve**: Based on the evaluation results and visualizations, you can iterate on your model, make necessary adjustments, and retrain it to improve its performance. TruLens allows you to easily compare different models and track their progress over time.\n",
|
24 |
-
"\n",
|
25 |
-
"By following these steps and leveraging the capabilities of TruLens, you can effectively evaluate your machine learning models and make informed decisions about their performance and potential enhancements."
|
26 |
-
]
|
27 |
-
},
|
28 |
-
{
|
29 |
-
"cell_type": "code",
|
30 |
-
"execution_count": 16,
|
31 |
-
"metadata": {},
|
32 |
-
"outputs": [],
|
33 |
-
"source": [
|
34 |
-
"from langchain_pinecone import PineconeVectorStore\n",
|
35 |
-
"from langchain_google_genai import GoogleGenerativeAIEmbeddings\n",
|
36 |
-
"from langchain_google_genai import ChatGoogleGenerativeAI\n",
|
37 |
-
"from langchain.chains import RetrievalQA\n",
|
38 |
-
"from langchain.document_loaders import PyPDFLoader\n",
|
39 |
-
"from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
|
40 |
-
"from langchain_openai import OpenAIEmbeddings\n",
|
41 |
-
"# from sentence_transformers import SentenceTransformer\n",
|
42 |
-
"from langchain_google_genai import GoogleGenerativeAIEmbeddings\n",
|
43 |
-
"\n",
|
44 |
-
"from langchain_google_genai import ChatGoogleGenerativeAI\n",
|
45 |
-
"from langchain_pinecone import PineconeVectorStore\n",
|
46 |
-
"from langchain.chains.question_answering import load_qa_chain\n",
|
47 |
-
"from langchain import PromptTemplate\n",
|
48 |
-
"from uuid import uuid4\n",
|
49 |
-
"from langchain_core.documents import Document\n",
|
50 |
-
"import getpass\n",
|
51 |
-
"import os\n",
|
52 |
-
"import google.generativeai as genai\n",
|
53 |
-
"from langchain.document_loaders import PyPDFLoader"
|
54 |
-
]
|
55 |
-
},
|
56 |
-
{
|
57 |
-
"cell_type": "markdown",
|
58 |
-
"metadata": {},
|
59 |
-
"source": [
|
60 |
-
"### Importing the documents needs to be loaded in Vector Database\n"
|
61 |
-
]
|
62 |
-
},
|
63 |
-
{
|
64 |
-
"cell_type": "code",
|
65 |
-
"execution_count": 2,
|
66 |
-
"metadata": {},
|
67 |
-
"outputs": [],
|
68 |
-
"source": [
|
69 |
-
"base_dir = os.getcwd()\n",
|
70 |
-
"file_name = \"Reliance.pdf\"\n",
|
71 |
-
"file_path = os.path.join(base_dir,file_name )\n",
|
72 |
-
"\n",
|
73 |
-
"def create_chunks(doc_to_chunk):\n",
|
74 |
-
" text_splitter = RecursiveCharacterTextSplitter(\n",
|
75 |
-
" chunk_size=500,\n",
|
76 |
-
" chunk_overlap=100,\n",
|
77 |
-
" length_function=len\n",
|
78 |
-
" )\n",
|
79 |
-
" return text_splitter.split_documents(doc_to_chunk)\n",
|
80 |
-
"\n",
|
81 |
-
"def load_pdf(path):\n",
|
82 |
-
" loader = PyPDFLoader(path)\n",
|
83 |
-
" return loader.load()\n",
|
84 |
-
"\n",
|
85 |
-
"def load_chunk_file(path):\n",
|
86 |
-
" doc = load_pdf(path)\n",
|
87 |
-
" return create_chunks(doc)\n",
|
88 |
-
"\n",
|
89 |
-
"chunks = load_chunk_file(file_path)"
|
90 |
-
]
|
91 |
-
},
|
92 |
-
{
|
93 |
-
"cell_type": "markdown",
|
94 |
-
"metadata": {},
|
95 |
-
"source": [
|
96 |
-
"### Loading the document into Vector Database"
|
97 |
-
]
|
98 |
-
},
|
99 |
-
{
|
100 |
-
"cell_type": "code",
|
101 |
-
"execution_count": 3,
|
102 |
-
"metadata": {},
|
103 |
-
"outputs": [
|
104 |
-
{
|
105 |
-
"data": {
|
106 |
-
"text/plain": [
|
107 |
-
"['0c2ac5d0-8ee3-4b7e-8846-2918154f1eb8',\n",
|
108 |
-
" 'aa5fc10a-7127-4f21-889f-28f4416f476b',\n",
|
109 |
-
" '1642ce19-52a7-47ba-b9f5-571427234765',\n",
|
110 |
-
" 'ab61277d-6c32-4c2b-9f16-462e1d91920b',\n",
|
111 |
-
" 'df66c977-49f0-4f0c-b15d-399bae4cd22f',\n",
|
112 |
-
" '2a7a3525-6032-4c9d-8841-8ae7ebf56850',\n",
|
113 |
-
" 'b43a8564-a85b-4119-b58d-575e45a42165',\n",
|
114 |
-
" '30ab4aba-7d2c-47b4-834c-59bc92f9ed74',\n",
|
115 |
-
" '623f618b-a7f2-4095-b115-8b410ea9eb9e',\n",
|
116 |
-
" 'c2ea7159-4de7-448e-aa69-1e9accff1850',\n",
|
117 |
-
" '5869be42-c13a-4370-bc3b-2007d09cb7bc',\n",
|
118 |
-
" 'e00749b7-b2a5-42f1-a16e-d6ed85849c91',\n",
|
119 |
-
" '3673b32b-14b0-479d-8791-12f22bd90ff8',\n",
|
120 |
-
" '919227ed-2b17-4b9c-a7d8-1de38c14d626',\n",
|
121 |
-
" 'e9af81a2-0753-4788-993d-b59cc2cada2c',\n",
|
122 |
-
" '872a0391-ad12-4eb5-baa5-fe6fe8ee659b',\n",
|
123 |
-
" '744cf651-e62d-4739-9531-a0dc8e4f1f07',\n",
|
124 |
-
" '645c60da-b40a-453a-b18f-4c06d314e221',\n",
|
125 |
-
" 'b4f052a5-475c-43ab-8252-b28793ccd5c4',\n",
|
126 |
-
" 'c40aea8d-d4a2-4af4-9054-97613d23e4a0',\n",
|
127 |
-
" 'ea57becd-f898-47bb-9ca5-9fe15cb20454',\n",
|
128 |
-
" 'd6b72ea5-eb18-47c0-8662-d4d211dfd10b',\n",
|
129 |
-
" '83cea095-1704-46b1-85bd-79888fdf7d86',\n",
|
130 |
-
" '189bdbb1-a31a-4e66-b5e8-59b1b4655398',\n",
|
131 |
-
" 'a34b2aed-81a9-4326-9ccb-1e3fcf3873a9',\n",
|
132 |
-
" '00180119-f5cf-41de-84b7-610b701383bf',\n",
|
133 |
-
" '53f9ecc4-679c-47f8-9627-f2f886ef42de',\n",
|
134 |
-
" 'be45dc55-d9ff-470c-9393-6cf96dc73c32',\n",
|
135 |
-
" '63d75c0b-020e-4f72-9cae-3f2e4e82b228',\n",
|
136 |
-
" 'c59b133d-c867-45be-963b-cb8f5df49802',\n",
|
137 |
-
" 'bdf3cd50-4d60-4395-95a4-cde7a5e32f81',\n",
|
138 |
-
" '29de5ec1-9910-45bd-8e1e-df565254ef73',\n",
|
139 |
-
" '92a57d8c-84d2-4c73-9b40-682ed42e3998',\n",
|
140 |
-
" '9954113f-64d7-4077-9097-1a64f2e6f096',\n",
|
141 |
-
" 'eced5907-1ec7-45c2-9f1f-036c4fb78d6c',\n",
|
142 |
-
" '2405c386-79f3-4337-8c40-db8b84bfedc2',\n",
|
143 |
-
" '144fde6f-710b-4e1b-ac2f-6f3af969375a',\n",
|
144 |
-
" '4929fd1e-f559-479b-974e-250a2bf2ac56',\n",
|
145 |
-
" 'a4671288-21f3-4210-adcc-82daba519c62',\n",
|
146 |
-
" '33219ac4-e102-4fdd-b453-8d911b6a0814',\n",
|
147 |
-
" '4945640d-595c-46b8-87dc-87182501d9bf',\n",
|
148 |
-
" '0f85529e-11e6-4120-b50c-7054b817d3aa',\n",
|
149 |
-
" 'af1e1941-2904-40f5-aabe-ae1f7901bc5a',\n",
|
150 |
-
" '3cca1def-8a18-4267-b04d-23fceab5faed',\n",
|
151 |
-
" '9a53a228-f505-46db-95cd-dba9f40663bf',\n",
|
152 |
-
" '0a5d4944-1a01-40f4-b557-330ed2e8a18a',\n",
|
153 |
-
" 'cd161a04-1ce2-406d-a43b-ea7efea0370c',\n",
|
154 |
-
" '4eddd6ce-b482-4466-9465-cda8c7c67d29',\n",
|
155 |
-
" '584a0ea1-20cd-46cb-b127-970449862997',\n",
|
156 |
-
" '4edbba63-e967-4864-be57-c1e195128021',\n",
|
157 |
-
" 'ed281465-721c-470a-8ae7-21cfcb8af944',\n",
|
158 |
-
" '143f581d-b09b-4afc-8d9c-eadb1c98e7bb',\n",
|
159 |
-
" 'ec300ec2-5e3a-49fb-a109-00ff9386fd51',\n",
|
160 |
-
" 'b233a907-d9b6-4d42-94cc-19013ca2ee22',\n",
|
161 |
-
" '96f478ba-d59c-4cec-b69b-f8153819c64b',\n",
|
162 |
-
" 'dee19024-32c4-437c-814f-65435855bc7d',\n",
|
163 |
-
" '35efa2fe-bfec-41b0-af06-c1421b618dfb',\n",
|
164 |
-
" 'a7f09dd4-0808-431f-9962-7781cec5d768',\n",
|
165 |
-
" '7dd9eb93-154f-49af-97cf-3acb30fe39f1',\n",
|
166 |
-
" 'ffb9ef79-92e3-4c24-b70d-717f9bb486ab',\n",
|
167 |
-
" 'd4d0fc60-eece-4cde-aebd-2d2eacb1a175',\n",
|
168 |
-
" '89a3c27b-332f-4598-b662-88460ebfea84',\n",
|
169 |
-
" '8b853629-e6a8-482c-9d6a-8100c8e11be1',\n",
|
170 |
-
" 'a16cbe9e-78a5-4998-9022-43941f5651ef',\n",
|
171 |
-
" '43198ea2-2cb6-41f9-968e-71aef1aa3092',\n",
|
172 |
-
" '201e741d-a2da-4f1c-bc0e-413cc264e15f',\n",
|
173 |
-
" '122b9da9-d278-4858-859f-a343d989c755',\n",
|
174 |
-
" 'e62610e0-fce8-484a-bb3f-c0acba538a20',\n",
|
175 |
-
" 'e33ce6e0-fa91-4ea2-895a-c5dfc57dda15',\n",
|
176 |
-
" '2feabd6a-0a10-4567-a00d-a175165795b7',\n",
|
177 |
-
" 'e2f71af2-c2f9-4949-825b-e9da02d33f56',\n",
|
178 |
-
" '51959195-4c81-4b4b-a928-91ae25c32cb7',\n",
|
179 |
-
" '40cbbbaa-0241-4ec7-8672-d9b4eec0b5e3',\n",
|
180 |
-
" 'e06501b9-6e70-4b46-a206-2d1bdbf8323a',\n",
|
181 |
-
" '09624885-5fd8-46e8-a984-5415088a2652',\n",
|
182 |
-
" 'ef2e77e7-a139-4d15-9415-c010cc348a58',\n",
|
183 |
-
" 'e0fde1e5-907b-4fd4-b7b4-76996ad804af',\n",
|
184 |
-
" '66658ffb-4311-4de6-8391-c58aa0539449',\n",
|
185 |
-
" '84bd7262-d5df-4b18-ae3f-0d16cfc9a305',\n",
|
186 |
-
" 'b6a1b8ca-d1b8-43ff-b58c-c3bd051cd6d1',\n",
|
187 |
-
" '4800b8ed-2d70-4251-a6b2-f3423c6179a0',\n",
|
188 |
-
" 'aecb53ac-b605-408f-896f-1969586ec320',\n",
|
189 |
-
" '6e67366e-95da-4cae-8efa-59f8a72de036',\n",
|
190 |
-
" '2b15f99a-f5db-4501-ada1-13b5e3608e7a',\n",
|
191 |
-
" 'cccc222d-79b9-48e0-b1ec-204e98eb57e3',\n",
|
192 |
-
" 'f56aeeb8-f883-4bf8-b7ba-5d7fa5c210cb',\n",
|
193 |
-
" '2aaa5c36-d138-48b3-bc79-5462a0b1d9f0',\n",
|
194 |
-
" '9bc822df-5102-4600-becc-344ac58ff8f1',\n",
|
195 |
-
" '485a6eeb-f603-4d68-88e5-17cb88385eee',\n",
|
196 |
-
" '9e3b0eb4-067c-4895-97e6-a4940e30c6e5',\n",
|
197 |
-
" 'd9ec6744-d756-4c70-8b22-a5d0ea1bc7f9',\n",
|
198 |
-
" '8dac6d17-163a-4186-bf2e-13c8a9d2c68b',\n",
|
199 |
-
" '6afeca7a-019f-44ed-99bc-4faaec6cea9d',\n",
|
200 |
-
" 'f22359e9-7fb8-465e-90da-9c31b2bc7044',\n",
|
201 |
-
" '10aefd98-f3cd-4a30-8153-ce4be52baa2e',\n",
|
202 |
-
" '444d3318-4b08-45f7-8706-26493b2ddd52',\n",
|
203 |
-
" '8247b43f-1ceb-45c3-95fa-96e33ce3f0ae',\n",
|
204 |
-
" '1dbd882d-fe93-4e43-b101-a2170b560645',\n",
|
205 |
-
" '661945bd-82da-4221-bd8c-189b28b8dd3c',\n",
|
206 |
-
" '0b406988-2b45-4c48-bfab-b6174b46cdbc',\n",
|
207 |
-
" 'fbd11f62-3d13-409b-9faa-887a305d6024',\n",
|
208 |
-
" '3d618eb3-d955-41eb-b0e8-e653b4955e4b',\n",
|
209 |
-
" 'b8ef62cf-2182-43cd-8035-d49efe89fc53',\n",
|
210 |
-
" '34b81bfe-4a05-40d2-b97f-1e760a4f9470',\n",
|
211 |
-
" '24e1dc11-8d7e-49ce-a68e-29424199f219',\n",
|
212 |
-
" '24ee8198-f1be-462c-a62b-bf50daa46456',\n",
|
213 |
-
" 'c7c11b34-7df2-477f-86ac-33499992c4c3',\n",
|
214 |
-
" 'bae84011-8d52-4eea-ade4-7e55fd9b6834',\n",
|
215 |
-
" '733a2c66-5857-4d0a-96fa-c14682d6ebfe',\n",
|
216 |
-
" '5ee5902e-e091-4295-b101-cf1550a474c0',\n",
|
217 |
-
" '417946bf-4757-499a-b6c9-1ff5c80e137f',\n",
|
218 |
-
" '4f7eeca1-5cab-42f8-a5cc-d42646c05692',\n",
|
219 |
-
" '1fcbfff3-c90a-403f-8253-9646b874fdae',\n",
|
220 |
-
" 'dde8af51-308b-44cc-923f-a0f14c7fc82c',\n",
|
221 |
-
" 'bd1410b3-a00d-40ae-99c1-232838ec2ca3',\n",
|
222 |
-
" '63f4b459-156c-4a26-97a5-f34f098b0b6a',\n",
|
223 |
-
" 'c3b75842-5879-4ff8-904d-045fe7918db6',\n",
|
224 |
-
" 'acdda0ae-50d7-4e6b-afda-7442052af76e',\n",
|
225 |
-
" 'de80e5c5-2dea-463b-85b5-2a43389b103b',\n",
|
226 |
-
" '0a1f41f7-b34c-47ad-9e11-1d01aad61f06',\n",
|
227 |
-
" '592a5b78-2901-4343-96c0-ed1cd08c218a',\n",
|
228 |
-
" '26e3415c-5919-4734-9236-018d5ae772e2']"
|
229 |
-
]
|
230 |
-
},
|
231 |
-
"execution_count": 3,
|
232 |
-
"metadata": {},
|
233 |
-
"output_type": "execute_result"
|
234 |
-
}
|
235 |
-
],
|
236 |
-
"source": [
|
237 |
-
"# Import the necessary modules\n",
|
238 |
-
"from langchain_pinecone import PineconeVectorStore\n",
|
239 |
-
"from langchain_core.documents import Document\n",
|
240 |
-
"from langchain_google_genai import GoogleGenerativeAIEmbeddings\n",
|
241 |
-
"from dotenv import load_dotenv\n",
|
242 |
-
"import os\n",
|
243 |
-
"from pinecone import Pinecone, ServerlessSpec\n",
|
244 |
-
"load_dotenv()\n",
|
245 |
-
"\n",
|
246 |
-
"# Initialize the Pinecone vector store\n",
|
247 |
-
"index_name = \"test-index\"\n",
|
248 |
-
"pc = Pinecone(\n",
|
249 |
-
" api_key=os.environ[\"PINECONE_API_KEY\"]) \n",
|
250 |
-
"if index_name not in pc.list_indexes().names():\n",
|
251 |
-
" pc.create_index(\n",
|
252 |
-
" name=index_name,\n",
|
253 |
-
" dimension=768,\n",
|
254 |
-
" metric='cosine', \n",
|
255 |
-
" spec=ServerlessSpec(\n",
|
256 |
-
" cloud='aws', # Specify your preferred cloud provider\n",
|
257 |
-
" region='us-east-1' # Specify your preferred region\n",
|
258 |
-
" )\n",
|
259 |
-
" )\n",
|
260 |
-
"\n",
|
261 |
-
"embeddings = GoogleGenerativeAIEmbeddings(model=\"models/embedding-001\", google_api_key=os.environ[\"GOOGLE_API_KEY\"])\n",
|
262 |
-
"vectorstore = PineconeVectorStore(index_name=index_name, embedding=embeddings)\n",
|
263 |
-
"uuids = [str(uuid4()) for _ in range(len(chunks))]\n",
|
264 |
-
"vectorstore.add_documents(documents=chunks, ids=uuids)\n",
|
265 |
-
"\n",
|
266 |
-
"\n"
|
267 |
-
]
|
268 |
-
},
|
269 |
-
{
|
270 |
-
"cell_type": "markdown",
|
271 |
-
"metadata": {},
|
272 |
-
"source": [
|
273 |
-
"### Create questions for the performance evaluation of RAG"
|
274 |
-
]
|
275 |
-
},
|
276 |
-
{
|
277 |
-
"cell_type": "code",
|
278 |
-
"execution_count": 8,
|
279 |
-
"metadata": {},
|
280 |
-
"outputs": [
|
281 |
-
{
|
282 |
-
"ename": "ModuleNotFoundError",
|
283 |
-
"evalue": "No module named 'llama_index.embeddings.gemini'",
|
284 |
-
"output_type": "error",
|
285 |
-
"traceback": [
|
286 |
-
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
287 |
-
"\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
|
288 |
-
"Cell \u001b[1;32mIn[8], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mllama_index\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mllms\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mgemini\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Gemini\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mllama_index\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01membeddings\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mgemini\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m GeminiEmbeddings\n",
|
289 |
-
"\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'llama_index.embeddings.gemini'"
|
290 |
-
]
|
291 |
-
}
|
292 |
-
],
|
293 |
-
"source": [
|
294 |
-
"from llama_index.llms.gemini import Gemini\n",
|
295 |
-
"from llama_index.embeddings.gemini import GeminiEmbeddings\n"
|
296 |
-
]
|
297 |
-
},
|
298 |
-
{
|
299 |
-
"cell_type": "code",
|
300 |
-
"execution_count": 28,
|
301 |
-
"metadata": {},
|
302 |
-
"outputs": [
|
303 |
-
{
|
304 |
-
"name": "stdout",
|
305 |
-
"output_type": "stream",
|
306 |
-
"text": [
|
307 |
-
"Unexpected exception formatting exception. Falling back to standard exception\n"
|
308 |
-
]
|
309 |
-
},
|
310 |
-
{
|
311 |
-
"name": "stderr",
|
312 |
-
"output_type": "stream",
|
313 |
-
"text": [
|
314 |
-
"Traceback (most recent call last):\n",
|
315 |
-
" File \"C:\\Users\\agshi\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\IPython\\core\\interactiveshell.py\", line 3508, in run_code\n",
|
316 |
-
" exec(code_obj, self.user_global_ns, self.user_ns)\n",
|
317 |
-
" File \"C:\\Users\\agshi\\AppData\\Local\\Temp\\ipykernel_38684\\3905429830.py\", line 3, in <module>\n",
|
318 |
-
" resp = Gemini(Model = \"gemini-1.5-flash\").complete(\"Write a poem about a magic backpack\")\n",
|
319 |
-
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
320 |
-
" File \"C:\\Users\\agshi\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\llama_index\\llms\\gemini\\base.py\", line 147, in __init__\n",
|
321 |
-
" model_meta = genai.get_model(model)\n",
|
322 |
-
" ^^^^^^^^^^^^^^^^^^^^^^\n",
|
323 |
-
" File \"C:\\Users\\agshi\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\google\\generativeai\\models.py\", line 55, in get_model\n",
|
324 |
-
" elif name.startswith(\"tunedModels/\"):\n",
|
325 |
-
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
326 |
-
" File \"C:\\Users\\agshi\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\google\\generativeai\\types\\model_types.py\", line 357, in make_model_name\n",
|
327 |
-
"TypeError: Invalid input type. Expected one of the following types: `str`, `Model`, or `TunedModel`.\n",
|
328 |
-
"\n",
|
329 |
-
"During handling of the above exception, another exception occurred:\n",
|
330 |
-
"\n",
|
331 |
-
"Traceback (most recent call last):\n",
|
332 |
-
" File \"C:\\Users\\agshi\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\IPython\\core\\interactiveshell.py\", line 2105, in showtraceback\n",
|
333 |
-
" stb = self.InteractiveTB.structured_traceback(\n",
|
334 |
-
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
335 |
-
" File \"C:\\Users\\agshi\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\IPython\\core\\ultratb.py\", line 1396, in structured_traceback\n",
|
336 |
-
" return FormattedTB.structured_traceback(\n",
|
337 |
-
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
338 |
-
" File \"C:\\Users\\agshi\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\IPython\\core\\ultratb.py\", line 1287, in structured_traceback\n",
|
339 |
-
" return VerboseTB.structured_traceback(\n",
|
340 |
-
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
341 |
-
" File \"C:\\Users\\agshi\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\IPython\\core\\ultratb.py\", line 1140, in structured_traceback\n",
|
342 |
-
" formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,\n",
|
343 |
-
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
344 |
-
" File \"C:\\Users\\agshi\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\IPython\\core\\ultratb.py\", line 1055, in format_exception_as_a_whole\n",
|
345 |
-
" frames.append(self.format_record(record))\n",
|
346 |
-
" ^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
347 |
-
" File \"C:\\Users\\agshi\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\IPython\\core\\ultratb.py\", line 955, in format_record\n",
|
348 |
-
" frame_info.lines, Colors, self.has_colors, lvals\n",
|
349 |
-
" ^^^^^^^^^^^^^^^^\n",
|
350 |
-
" File \"C:\\Users\\agshi\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\IPython\\core\\ultratb.py\", line 778, in lines\n",
|
351 |
-
" return self._sd.lines\n",
|
352 |
-
" ^^^^^^^^^^^^^^\n",
|
353 |
-
" File \"C:\\Users\\agshi\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\stack_data\\utils.py\", line 145, in cached_property_wrapper\n",
|
354 |
-
" value = obj.__dict__[self.func.__name__] = self.func(obj)\n",
|
355 |
-
" ^^^^^^^^^^^^^^\n",
|
356 |
-
" File \"C:\\Users\\agshi\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\stack_data\\core.py\", line 734, in lines\n",
|
357 |
-
" pieces = self.included_pieces\n",
|
358 |
-
" ^^^^^^^^^^^^^^^^^^^^\n",
|
359 |
-
" File \"C:\\Users\\agshi\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\stack_data\\utils.py\", line 145, in cached_property_wrapper\n",
|
360 |
-
" value = obj.__dict__[self.func.__name__] = self.func(obj)\n",
|
361 |
-
" ^^^^^^^^^^^^^^\n",
|
362 |
-
" File \"C:\\Users\\agshi\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\stack_data\\core.py\", line 681, in included_pieces\n",
|
363 |
-
" pos = scope_pieces.index(self.executing_piece)\n",
|
364 |
-
" ^^^^^^^^^^^^^^^^^^^^\n",
|
365 |
-
" File \"C:\\Users\\agshi\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\stack_data\\utils.py\", line 145, in cached_property_wrapper\n",
|
366 |
-
" value = obj.__dict__[self.func.__name__] = self.func(obj)\n",
|
367 |
-
" ^^^^^^^^^^^^^^\n",
|
368 |
-
" File \"C:\\Users\\agshi\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\stack_data\\core.py\", line 660, in executing_piece\n",
|
369 |
-
" return only(\n",
|
370 |
-
" ^^^^^\n",
|
371 |
-
" File \"C:\\Users\\agshi\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\executing\\executing.py\", line 116, in only\n",
|
372 |
-
" raise NotOneValueFound('Expected one value, found 0')\n",
|
373 |
-
"executing.executing.NotOneValueFound: Expected one value, found 0\n"
|
374 |
-
]
|
375 |
-
}
|
376 |
-
],
|
377 |
-
"source": [
|
378 |
-
"from dotenv import load_dotenv\n",
|
379 |
-
"load_dotenv()\n",
|
380 |
-
"resp = Gemini(Model = \"gemini-1.5-flash\").complete(\"Write a poem about a magic backpack\")\n",
|
381 |
-
"print(resp)"
|
382 |
-
]
|
383 |
-
},
|
384 |
-
{
|
385 |
-
"cell_type": "code",
|
386 |
-
"execution_count": 20,
|
387 |
-
"metadata": {},
|
388 |
-
"outputs": [],
|
389 |
-
"source": [
|
390 |
-
"GOOGLE_API_KEY = os.environ[\"GOOGLE_API_KEY\"]"
|
391 |
-
]
|
392 |
-
},
|
393 |
-
{
|
394 |
-
"cell_type": "code",
|
395 |
-
"execution_count": 4,
|
396 |
-
"metadata": {},
|
397 |
-
"outputs": [],
|
398 |
-
"source": [
|
399 |
-
"questions = [\n",
|
400 |
-
" \"What is the company's registered office address?\",\n",
|
401 |
-
" \"What is the phone number of the company?\",\n",
|
402 |
-
" \"When was the company established?\",\n",
|
403 |
-
" \"Who is the regulatory authority for the company?\",\n",
|
404 |
-
" \"What is the website of the company?\",\n",
|
405 |
-
" \"What is the CIN (Corporate Identification Number) of the company?\",\n",
|
406 |
-
" \"Who is the CEO of the company?\",\n",
|
407 |
-
" \"What is the company's market capitalization?\",\n",
|
408 |
-
" \"What are the major products or services offered by the company?\",\n",
|
409 |
-
" \"What is the company's financial performance in the last fiscal year?\"\n",
|
410 |
-
"]"
|
411 |
-
]
|
412 |
-
},
|
413 |
-
{
|
414 |
-
"cell_type": "code",
|
415 |
-
"execution_count": 13,
|
416 |
-
"metadata": {},
|
417 |
-
"outputs": [
|
418 |
-
{
|
419 |
-
"name": "stdout",
|
420 |
-
"output_type": "stream",
|
421 |
-
"text": [
|
422 |
-
"✅ In Context Relevance, input source will be set to __record__.main_input or `Select.RecordInput` .\n",
|
423 |
-
"✅ In Context Relevance, input statement will be set to __record__.app.query.rets.source_nodes[:].node.text .\n",
|
424 |
-
"✅ In Answer Relevance, input prompt will be set to __record__.main_input or `Select.RecordInput` .\n",
|
425 |
-
"✅ In Answer Relevance, input context will be set to __record__.main_output or `Select.RecordOutput` .\n",
|
426 |
-
"✅ In Context Relevance, input prompt will be set to __record__.main_input or `Select.RecordInput` .\n",
|
427 |
-
"✅ In Context Relevance, input context will be set to __record__.app.query.rets.source_nodes[:].node.text .\n"
|
428 |
-
]
|
429 |
-
}
|
430 |
-
],
|
431 |
-
"source": [
|
432 |
-
"from trulens_eval.feedback.provider.langchain import Langchain\n",
|
433 |
-
"from trulens_eval.feedback.provider.hugs import Huggingface\n",
|
434 |
-
"from trulens_eval import Feedback\n",
|
435 |
-
"from trulens_eval import TruLlama\n",
|
436 |
-
"context_selection = TruLlama.select_source_nodes().node.text\n",
|
437 |
-
"provider = Huggingface()\n",
|
438 |
-
"import numpy as np\n",
|
439 |
-
"huggingface_provider = Huggingface()\n",
|
440 |
-
"# Define a groundedness feedback function\n",
|
441 |
-
"f_groundedness = (\n",
|
442 |
-
" Feedback(huggingface_provider.groundedness_measure_with_nli,\n",
|
443 |
-
" name=\"Context Relevance\")\n",
|
444 |
-
" .on_input()\n",
|
445 |
-
" .on(context_selection)\n",
|
446 |
-
" .aggregate(np.mean)\n",
|
447 |
-
")\n",
|
448 |
-
"# Question/answer relevance between overall question and answer.\n",
|
449 |
-
"f_answer_relevance = Feedback(\n",
|
450 |
-
" huggingface_provider.context_relevance,\n",
|
451 |
-
" name=\"Answer Relevance\"\n",
|
452 |
-
").on_input_output()\n",
|
453 |
-
"\n",
|
454 |
-
"# Context relevance between question and each context chunk.\n",
|
455 |
-
"f_context_relevance = (\n",
|
456 |
-
" Feedback(huggingface_provider.context_relevance,\n",
|
457 |
-
" name=\"Context Relevance\")\n",
|
458 |
-
" .on_input()\n",
|
459 |
-
" .on(context_selection)\n",
|
460 |
-
" .aggregate(np.mean)\n",
|
461 |
-
")"
|
462 |
-
]
|
463 |
-
},
|
464 |
-
{
|
465 |
-
"cell_type": "code",
|
466 |
-
"execution_count": 12,
|
467 |
-
"metadata": {},
|
468 |
-
"outputs": [],
|
469 |
-
"source": [
|
470 |
-
"from trulens_eval.feedback.provider.hugs import Huggingface\n",
|
471 |
-
"huggingface_provider = Huggingface()"
|
472 |
-
]
|
473 |
-
},
|
474 |
-
{
|
475 |
-
"cell_type": "code",
|
476 |
-
"execution_count": 14,
|
477 |
-
"metadata": {},
|
478 |
-
"outputs": [],
|
479 |
-
"source": [
|
480 |
-
"def generate_answer(query, vector_store, template):\n",
|
481 |
-
" # Use LangChain's RetrievalQA to query the vector store and generate the answer\n",
|
482 |
-
" qa_chain = RetrievalQA.from_chain_type(\n",
|
483 |
-
" llm=ChatGoogleGenerativeAI(model=\"gemini-1.5-flash\"),\n",
|
484 |
-
" chain_type=\"stuff\",\n",
|
485 |
-
" retriever=vector_store.as_retriever(search_type=\"similarity\")\n",
|
486 |
-
" )\n",
|
487 |
-
"\n",
|
488 |
-
" # Format the prompt with the retrieved context and the query\n",
|
489 |
-
" prompt = template.format(context=\"{context}\", question=query)\n",
|
490 |
-
" \n",
|
491 |
-
" # Generate the answer by running the chain with the combined prompt\n",
|
492 |
-
" answer = qa_chain.run(query=query) # Pass 'input' as keyword argument with the formatted prompt\n",
|
493 |
-
" return answer"
|
494 |
-
]
|
495 |
-
},
|
496 |
-
{
|
497 |
-
"cell_type": "code",
|
498 |
-
"execution_count": null,
|
499 |
-
"metadata": {},
|
500 |
-
"outputs": [],
|
501 |
-
"source": [
|
502 |
-
"generate_answer(\n",
|
503 |
-
" \"How do you create your AI portfolio?\")"
|
504 |
-
]
|
505 |
-
},
|
506 |
-
{
|
507 |
-
"cell_type": "code",
|
508 |
-
"execution_count": 15,
|
509 |
-
"metadata": {},
|
510 |
-
"outputs": [
|
511 |
-
{
|
512 |
-
"ename": "ValidationError",
|
513 |
-
"evalue": "2 validation errors for TruLlama\napp.is-instance[BaseQueryEngine]\n Input should be an instance of BaseQueryEngine [type=is_instance_of, input_value=<function generate_answer at 0x0000019D4B98EFC0>, input_type=function]\n For further information visit https://errors.pydantic.dev/2.8/v/is_instance_of\napp.is-instance[BaseChatEngine]\n Input should be an instance of BaseChatEngine [type=is_instance_of, input_value=<function generate_answer at 0x0000019D4B98EFC0>, input_type=function]\n For further information visit https://errors.pydantic.dev/2.8/v/is_instance_of",
|
514 |
-
"output_type": "error",
|
515 |
-
"traceback": [
|
516 |
-
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
517 |
-
"\u001b[1;31mValidationError\u001b[0m Traceback (most recent call last)",
|
518 |
-
"Cell \u001b[1;32mIn[15], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtrulens_eval\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m TruLlama\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtrulens_eval\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m FeedbackMode\n\u001b[1;32m----> 3\u001b[0m tru_recorder \u001b[38;5;241m=\u001b[39m \u001b[43mTruLlama\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m 4\u001b[0m \u001b[43m \u001b[49m\u001b[43mgenerate_answer\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 5\u001b[0m \u001b[43m \u001b[49m\u001b[43mapp_id\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mApp_1\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m 6\u001b[0m \u001b[43m \u001b[49m\u001b[43mfeedbacks\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\n\u001b[0;32m 7\u001b[0m \u001b[43m \u001b[49m\u001b[43mf_groundedness\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 8\u001b[0m \u001b[43m \u001b[49m\u001b[43mf_answer_relevance\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 9\u001b[0m \u001b[43m \u001b[49m\u001b[43mf_context_relevance\u001b[49m\n\u001b[0;32m 10\u001b[0m \u001b[43m \u001b[49m\u001b[43m]\u001b[49m\n\u001b[0;32m 11\u001b[0m \u001b[43m)\u001b[49m\n",
|
519 |
-
"File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\trulens_eval\\tru_llama.py:314\u001b[0m, in \u001b[0;36mTruLlama.__init__\u001b[1;34m(self, app, **kwargs)\u001b[0m\n\u001b[0;32m 311\u001b[0m kwargs[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mroot_class\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m Class\u001b[38;5;241m.\u001b[39mof_object(app) \u001b[38;5;66;03m# TODO: make class property\u001b[39;00m\n\u001b[0;32m 312\u001b[0m kwargs[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124minstrument\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m LlamaInstrument(app\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m)\n\u001b[1;32m--> 314\u001b[0m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__init__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
|
520 |
-
"File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\trulens_eval\\app.py:564\u001b[0m, in \u001b[0;36mApp.__init__\u001b[1;34m(self, tru, feedbacks, **kwargs)\u001b[0m\n\u001b[0;32m 559\u001b[0m kwargs[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mfeedbacks\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m feedbacks\n\u001b[0;32m 560\u001b[0m kwargs[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mrecording_contexts\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m contextvars\u001b[38;5;241m.\u001b[39mContextVar(\n\u001b[0;32m 561\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrecording_contexts\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 562\u001b[0m )\n\u001b[1;32m--> 564\u001b[0m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__init__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 566\u001b[0m app \u001b[38;5;241m=\u001b[39m kwargs[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mapp\u001b[39m\u001b[38;5;124m'\u001b[39m]\n\u001b[0;32m 567\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mapp \u001b[38;5;241m=\u001b[39m app\n",
|
521 |
-
"File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\trulens_eval\\schema\\app.py:97\u001b[0m, in \u001b[0;36mAppDefinition.__init__\u001b[1;34m(self, app_id, tags, metadata, feedback_mode, app_extra_json, **kwargs)\u001b[0m\n\u001b[0;32m 94\u001b[0m kwargs[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mmetadata\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m {}\n\u001b[0;32m 95\u001b[0m kwargs[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mapp_extra_json\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m app_extra_json \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mdict\u001b[39m()\n\u001b[1;32m---> 97\u001b[0m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__init__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 99\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m app_id \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m 100\u001b[0m app_id \u001b[38;5;241m=\u001b[39m obj_id_of_obj(obj\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmodel_dump(), prefix\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mapp\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
|
522 |
-
"File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\trulens_eval\\utils\\pyschema.py:686\u001b[0m, in \u001b[0;36mWithClassInfo.__init__\u001b[1;34m(self, class_info, obj, cls, *args, **kwargs)\u001b[0m\n\u001b[0;32m 682\u001b[0m class_info \u001b[38;5;241m=\u001b[39m Class\u001b[38;5;241m.\u001b[39mof_class(\u001b[38;5;28mcls\u001b[39m, with_bases\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[0;32m 684\u001b[0m kwargs[CLASS_INFO] \u001b[38;5;241m=\u001b[39m class_info\n\u001b[1;32m--> 686\u001b[0m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__init__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
|
523 |
-
"File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\pydantic\\main.py:193\u001b[0m, in \u001b[0;36mBaseModel.__init__\u001b[1;34m(self, **data)\u001b[0m\n\u001b[0;32m 191\u001b[0m \u001b[38;5;66;03m# `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks\u001b[39;00m\n\u001b[0;32m 192\u001b[0m __tracebackhide__ \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[1;32m--> 193\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m__pydantic_validator__\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalidate_python\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdata\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mself_instance\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m)\u001b[49m\n",
|
524 |
-
"\u001b[1;31mValidationError\u001b[0m: 2 validation errors for TruLlama\napp.is-instance[BaseQueryEngine]\n Input should be an instance of BaseQueryEngine [type=is_instance_of, input_value=<function generate_answer at 0x0000019D4B98EFC0>, input_type=function]\n For further information visit https://errors.pydantic.dev/2.8/v/is_instance_of\napp.is-instance[BaseChatEngine]\n Input should be an instance of BaseChatEngine [type=is_instance_of, input_value=<function generate_answer at 0x0000019D4B98EFC0>, input_type=function]\n For further information visit https://errors.pydantic.dev/2.8/v/is_instance_of"
|
525 |
-
]
|
526 |
-
}
|
527 |
-
],
|
528 |
-
"source": [
|
529 |
-
"from trulens_eval import TruLlama\n",
|
530 |
-
"from trulens_eval import FeedbackMode\n",
|
531 |
-
"tru_recorder = TruLlama(\n",
|
532 |
-
" generate_answer,\n",
|
533 |
-
" app_id=\"App_1\",\n",
|
534 |
-
" feedbacks=[\n",
|
535 |
-
" f_groundedness,\n",
|
536 |
-
" f_answer_relevance,\n",
|
537 |
-
" f_context_relevance\n",
|
538 |
-
" ]\n",
|
539 |
-
")"
|
540 |
-
]
|
541 |
-
},
|
542 |
-
{
|
543 |
-
"cell_type": "code",
|
544 |
-
"execution_count": null,
|
545 |
-
"metadata": {},
|
546 |
-
"outputs": [],
|
547 |
-
"source": []
|
548 |
-
}
|
549 |
-
],
|
550 |
-
"metadata": {
|
551 |
-
"kernelspec": {
|
552 |
-
"display_name": "Python 3",
|
553 |
-
"language": "python",
|
554 |
-
"name": "python3"
|
555 |
-
},
|
556 |
-
"language_info": {
|
557 |
-
"codemirror_mode": {
|
558 |
-
"name": "ipython",
|
559 |
-
"version": 3
|
560 |
-
},
|
561 |
-
"file_extension": ".py",
|
562 |
-
"mimetype": "text/x-python",
|
563 |
-
"name": "python",
|
564 |
-
"nbconvert_exporter": "python",
|
565 |
-
"pygments_lexer": "ipython3",
|
566 |
-
"version": "3.11.9"
|
567 |
-
}
|
568 |
-
},
|
569 |
-
"nbformat": 4,
|
570 |
-
"nbformat_minor": 2
|
571 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|