code: """Llama Dataset Class."""
  
  import asyncio
  import time
  from typing import List, Optional
  
  from llama_index.core.base.base_query_engine import BaseQueryEngine
  from llama_index.core.bridge.pydantic import Field
  from llama_index.core.llama_dataset.base import (
      BaseLlamaDataExample,
      BaseLlamaDataset,
      BaseLlamaExamplePrediction,
      BaseLlamaPredictionDataset,
      CreatedBy,
  )
  from pandas import DataFrame as PandasDataFrame
  
  
  class RagExamplePrediction(BaseLlamaExamplePrediction):
      """RAG example prediction class.
  
      Args:
          response (str): The response generated by the LLM.
          contexts (Optional[List[str]]): The retrieved context (text) for generating
                                          response.
      """
  
      response: str = Field(
          default_factory=str,
          description="The generated (predicted) response that can be compared to a reference (ground-truth) answer.",
      )
      contexts: Optional[List[str]] = Field(
          default_factory=None,
          description="The contexts in raw text form used to generate the response.",
      )
  
      @property
      def class_name(self) -> str:
          """Data example class name."""
          return "RagExamplePrediction"
  
  
  class LabelledRagDataExample(BaseLlamaDataExample):
      """RAG example class. Analogous to traditional ML datasets, this dataset contains
      the "features" (i.e., query + context) to make a prediction and the "label" (i.e., response)
      to evaluate the prediction.
  
      Args:
          query (str): The user query
          query_by (CreatedBy): Query generated by human or ai (model-name)
          reference_contexts (Optional[List[str]]): The contexts used for response
          reference_answer ([str]): Reference answer to the query. An answer
                                      that would receive full marks upon evaluation.
          reference_answer_by: The reference answer generated by human or ai (model-name).
      """
  
      query: str = Field(
          default_factory=str, description="The user query for the example."
      )
      query_by: Optional[CreatedBy] = Field(
          default=None, description="What generated the query."
      )
      reference_contexts: Optional[List[str]] = Field(
          default_factory=None,
          description="The contexts used to generate the reference answer.",
      )
      reference_answer: str = Field(
          default_factory=str,
          description="The reference (ground-truth) answer to the example.",
      )
      reference_answer_by: Optional[CreatedBy] = Field(
          default=None, description="What generated the reference answer."
      )
  
      @property
      def class_name(self) -> str:
          """Data example class name."""
          return "LabelledRagDataExample"
  
  
  class RagPredictionDataset(BaseLlamaPredictionDataset):
      """RagDataset class."""
  
      _prediction_type = RagExamplePrediction
  
      def to_pandas(self) -> PandasDataFrame:
          """Create pandas dataframe."""
          data = {}
          if self.predictions:
              data = {
                  "response": [t.response for t in self.predictions],
                  "contexts": [t.contexts for t in self.predictions],
              }
  
          return PandasDataFrame(data)
  
      @property
      def class_name(self) -> str:
          """Class name."""
          return "RagPredictionDataset"
  
  
  class LabelledRagDataset(BaseLlamaDataset[BaseQueryEngine]):
      """RagDataset class."""
  
      _example_type = LabelledRagDataExample
  
      def to_pandas(self) -> PandasDataFrame:
          """Create pandas dataframe."""
          data = {
              "query": [t.query for t in self.examples],
              "reference_contexts": [t.reference_contexts for t in self.examples],
              "reference_answer": [t.reference_answer for t in self.examples],
              "reference_answer_by": [str(t.reference_answer_by) for t in self.examples],
              "query_by": [str(t.query_by) for t in self.examples],
          }
  
          return PandasDataFrame(data)
  
      async def _apredict_example(
          self,
          predictor: BaseQueryEngine,
          example: LabelledRagDataExample,
          sleep_time_in_seconds: int,
      ) -> RagExamplePrediction:
          """Async predict RAG example with a query engine."""
          await asyncio.sleep(sleep_time_in_seconds)
          response = await predictor.aquery(example.query)
          return RagExamplePrediction(
              response=str(response), contexts=[s.text for s in response.source_nodes]
          )
  
      def _predict_example(
          self,
          predictor: BaseQueryEngine,
          example: LabelledRagDataExample,
          sleep_time_in_seconds: int = 0,
      ) -> RagExamplePrediction:
          """Predict RAG example with a query engine."""
          time.sleep(sleep_time_in_seconds)
          response = predictor.query(example.query)
          return RagExamplePrediction(
              response=str(response), contexts=[s.text for s in response.source_nodes]
          )
  
      def _construct_prediction_dataset(
          self, predictions: List[RagExamplePrediction]
      ) -> RagPredictionDataset:
          """Construct prediction dataset."""
          return RagPredictionDataset(predictions=predictions)
  
      @property
      def class_name(self) -> str:
          """Class name."""
          return "LabelledRagDataset"
  
  
  # British English + American English
  LabeledRagDataExample = LabelledRagDataExample
  LabeledRagDataset = LabelledRagDataset
  
apis: llama_index.core.bridge.pydantic.Field
extract_api: [((764, 909), 'llama_index.core.bridge.pydantic.Field', 'Field', ([], {'default_factory': 'str', 'description': '"""The generated (predicted) response that can be compared to a reference (ground-truth) answer."""'}), "(default_factory=str, description=\n    'The generated (predicted) response that can be compared to a reference (ground-truth) answer.'\n    )\n", (769, 909), False, 'from llama_index.core.bridge.pydantic import Field\n'), ((959, 1067), 'llama_index.core.bridge.pydantic.Field', 'Field', ([], {'default_factory': 'None', 'description': '"""The contexts in raw text form used to generate the response."""'}), "(default_factory=None, description=\n    'The contexts in raw text form used to generate the response.')\n", (964, 1067), False, 'from llama_index.core.bridge.pydantic import Field\n'), ((1955, 2028), 'llama_index.core.bridge.pydantic.Field', 'Field', ([], {'default_factory': 'str', 'description': '"""The user query for the example."""'}), "(default_factory=str, description='The user query for the example.')\n", (1960, 2028), False, 'from llama_index.core.bridge.pydantic import Field\n'), ((2079, 2139), 'llama_index.core.bridge.pydantic.Field', 'Field', ([], {'default': 'None', 'description': '"""What generated the query."""'}), "(default=None, description='What generated the query.')\n", (2084, 2139), False, 'from llama_index.core.bridge.pydantic import Field\n'), ((2200, 2299), 'llama_index.core.bridge.pydantic.Field', 'Field', ([], {'default_factory': 'None', 'description': '"""The contexts used to generate the reference answer."""'}), "(default_factory=None, description=\n    'The contexts used to generate the reference answer.')\n", (2205, 2299), False, 'from llama_index.core.bridge.pydantic import Field\n'), ((2346, 2444), 'llama_index.core.bridge.pydantic.Field', 'Field', ([], {'default_factory': 'str', 'description': '"""The reference (ground-truth) answer to the example."""'}), "(default_factory=str, description=\n    'The reference (ground-truth) answer to the example.')\n", (2351, 2444), False, 'from llama_index.core.bridge.pydantic import Field\n'), ((2510, 2581), 'llama_index.core.bridge.pydantic.Field', 'Field', ([], {'default': 'None', 'description': '"""What generated the reference answer."""'}), "(default=None, description='What generated the reference answer.')\n", (2515, 2581), False, 'from llama_index.core.bridge.pydantic import Field\n'), ((3172, 3193), 'pandas.DataFrame', 'PandasDataFrame', (['data'], {}), '(data)\n', (3187, 3193), True, 'from pandas import DataFrame as PandasDataFrame\n'), ((3935, 3956), 'pandas.DataFrame', 'PandasDataFrame', (['data'], {}), '(data)\n', (3950, 3956), True, 'from pandas import DataFrame as PandasDataFrame\n'), ((4702, 4735), 'time.sleep', 'time.sleep', (['sleep_time_in_seconds'], {}), '(sleep_time_in_seconds)\n', (4712, 4735), False, 'import time\n'), ((4224, 4260), 'asyncio.sleep', 'asyncio.sleep', (['sleep_time_in_seconds'], {}), '(sleep_time_in_seconds)\n', (4237, 4260), False, 'import asyncio\n')]