get_ipython().run_line_magic('load_ext', 'autoreload')
get_ipython().run_line_magic('autoreload', '2')

import sys
import pkg_resources 

try: # When on Colab, let's install pyserini, Pytorch, and Faiss
    import google.colab
    repo_path = 'dspy'
    get_ipython().system('git -C $repo_path pull origin || git clone https://github.com/stanfordnlp/dspy $repo_path')
    get_ipython().run_line_magic('cd', '$repo_path')
    get_ipython().system('pip install -e .')
    if not "pyserini" in {pkg.key for pkg in pkg_resources.working_set}:
        get_ipython().system('pip install pyserini')
    if not "torch" in {pkg.key for pkg in pkg_resources.working_set}:
        get_ipython().system('pip install torch')
    if not "faiss-cpu" in {pkg.key for pkg in pkg_resources.working_set}:
        get_ipython().system('pip install faiss-cpu')
except:
    repo_path = '.'
    if not "dspy-ai" in {pkg.key for pkg in pkg_resources.working_set}:
        get_ipython().system('pip install -U pip')
        get_ipython().system('pip install dspy-ai')

if repo_path not in sys.path:
    sys.path.append(repo_path)

import dspy



pys_ret_prebuilt = 
dspy.Pyserini(index='beir-v1.0.0-nfcorpus.contriever-msmarco', query_encoder='facebook/contriever-msmarco', id_field='_id', text_fields=['title', 'text'])