class_docstr: This Completion Function uses embeddings to retrieve the top k relevant docs from a dataset to the prompt, then adds them to the context before calling the completion.
class_docstr_tok: ["This","Completion","Function","uses","embeddings","to","retrieve","the","top","k","relevant","docs","from","a","dataset","to","the","prompt",",","then","adds","them","to","the","context","before","calling","the","completion","."]
docstr: Args:
      prompt: The prompt to complete, in either text string or Chat format.
      kwargs: Additional arguments to pass to the completion function call method.
docstr_tok: ["Args",":","prompt",":","The","prompt","to","complete",",","in","either","text","string","or","Chat","format",".","kwargs",":","Additional","arguments","to","pass","to","the","completion","function","call","method","."]
code: def __call__(
      self, prompt: Union[str, list[dict]], **kwargs: Any
  ) -> RetrievalCompletionResult:
      """
      Args:
          prompt: The prompt to complete, in either text string or Chat format.
          kwargs: Additional arguments to pass to the completion function call method.
      """
      # Embed the prompt
      embedded_prompt = (
          client.embeddings.create(
              model=self.embedding_model,
              input=CompletionPrompt(prompt).to_formatted_prompt(),
          )
          .data[0]
          .embedding
      )
  
      embs = self.embeddings_df["embedding"].to_list()
  
      # Compute the cosine similarity between the prompt and the embeddings
      topk = " ".join(
          self.embeddings_df.iloc[
              find_top_k_closest_embeddings(
                  embedded_prompt, embs, k=self.k
              )
          ].text.values
      )
  
      prompt = ChatCompletionPrompt(prompt).to_formatted_prompt()
      retrieval_prompt = [
          {"role": "system", "content": self.retrieval_template + topk}
      ] + prompt
  
      answer = self.completion_fn_instance(
          prompt=retrieval_prompt, **kwargs
      ).get_completions()[0]
      record_sampling(prompt=retrieval_prompt, sampled=answer)
      return RetrievalCompletionResult(answer)
  
file_path: evals/evals/completion_fns/retrieval.py
imports: import os
  from ast import literal_eval
  from typing import Any, Optional, Union
  import numpy
  import pandas
  from openai import OpenAI
  from evals.api import CompletionFn, CompletionResult
  from evals.prompt.base import ChatCompletionPrompt, CompletionPrompt
  from evals.record import record_sampling
  from evals.registry import Registry
label_desc: Use image node_id 2 for calling the RetrievalCompletionFn obj's underlying member method code with example usage: obj.__call__(prompt) and returns: RetrievalCompletionResult