|
162 | 162 | "from ragas.run_config import RunConfig\n", |
163 | 163 | "from ragas.metrics.base import MetricWithLLM, MetricWithEmbeddings\n", |
164 | 164 | "\n", |
| 165 | + "\n", |
165 | 166 | "# util function to init Ragas Metrics\n", |
166 | 167 | "def init_ragas_metrics(metrics, llm, embedding):\n", |
167 | 168 | " for metric in metrics:\n", |
|
183 | 184 | "from langchain_openai.chat_models import ChatOpenAI\n", |
184 | 185 | "from langchain_openai.embeddings import OpenAIEmbeddings\n", |
185 | 186 | "\n", |
186 | | - "# wrappers \n", |
| 187 | + "# wrappers\n", |
187 | 188 | "from ragas.llms import LangchainLLMWrapper\n", |
188 | 189 | "from ragas.embeddings import LangchainEmbeddingsWrapper\n", |
189 | 190 | "\n", |
|
368 | 369 | "metadata": {}, |
369 | 370 | "outputs": [], |
370 | 371 | "source": [ |
371 | | - "# the logic of the dummy application is \n", |
| 372 | + "# the logic of the dummy application is\n", |
372 | 373 | "# given a question fetch the correspoinding contexts and answers from a dict\n", |
373 | 374 | "\n", |
374 | 375 | "import hashlib\n", |
375 | 376 | "\n", |
| 377 | + "\n", |
376 | 378 | "def hash_string(input_string):\n", |
377 | 379 | " return hashlib.sha256(input_string.encode()).hexdigest()\n", |
378 | 380 | "\n", |
| 381 | + "\n", |
379 | 382 | "q_to_c = {} # map between question and context\n", |
380 | 383 | "q_to_a = {} # map between question and answer\n", |
381 | 384 | "for row in amnesty_qa:\n", |
|
394 | 397 | "# if your running this in a notebook - please run this cell\n", |
395 | 398 | "# to manage asyncio event loops\n", |
396 | 399 | "import nest_asyncio\n", |
| 400 | + "\n", |
397 | 401 | "nest_asyncio.apply()" |
398 | 402 | ] |
399 | 403 | }, |
|
407 | 411 | "from langfuse.decorators import observe, langfuse_context\n", |
408 | 412 | "from asyncio import run\n", |
409 | 413 | "\n", |
| 414 | + "\n", |
410 | 415 | "@observe()\n", |
411 | 416 | "def retriver(question: str):\n", |
412 | 417 | " return q_to_c[question]\n", |
413 | 418 | "\n", |
| 419 | + "\n", |
414 | 420 | "@observe()\n", |
415 | 421 | "def generator(question):\n", |
416 | 422 | " return q_to_a[question]\n", |
417 | 423 | "\n", |
| 424 | + "\n", |
418 | 425 | "@observe()\n", |
419 | 426 | "def rag_pipeline(question):\n", |
420 | 427 | " q_hash = hash_string(question)\n", |
|
424 | 431 | " # score the runs\n", |
425 | 432 | " score = run(score_with_ragas(question, contexts, answer=generated_answer))\n", |
426 | 433 | " for s in score:\n", |
427 | | - " langfuse_context.score_current_trace(\n", |
428 | | - " name=s,\n", |
429 | | - " value=score[s]\n", |
430 | | - " )\n", |
| 434 | + " langfuse_context.score_current_trace(name=s, value=score[s])\n", |
431 | 435 | " return generated_answer" |
432 | 436 | ] |
433 | 437 | }, |
|
0 commit comments