import dspy

from dspy.evaluate.evaluate import Evaluate
from dspy.teleprompt import BootstrapFewShotWithRandomSearch

colbertv2 = dspy.ColBERTv2(url='http://20.102.90.50:2017/wiki17_abstracts')

dspy.configure(rm=colbertv2)



from langchain_openai import OpenAI
from langchain.globals import set_llm_cache
from langchain.cache import SQLiteCache

set_llm_cache(SQLiteCache(database_path="cache.db"))

llm = OpenAI(model_name="gpt-3.5-turbo-instruct", temperature=0)
retrieve = lambda x: dspy.Retrieve(k=5)(x["question"]).passages




from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough

prompt = PromptTemplate.from_template("Given {context}, answer the question `{question}` as a tweet.")

vanilla_chain = RunnablePassthrough.assign(context=retrieve) | prompt | llm | StrOutputParser()




from dspy.predict.langchain import LangChainPredict, LangChainModule

zeroshot_chain = RunnablePassthrough.assign(context=retrieve) | LangChainPredict(prompt, llm) | StrOutputParser()
zeroshot_chain = LangChainModule(zeroshot_chain)  # then wrap the chain in a DSPy module.



question = "In what region was Eddy Mazzoleni born?"

zeroshot_chain.invoke({"question": question})




from tweet_metric import metric, trainset, valset, devset

len(trainset), len(valset), len(devset)



evaluate = 
Evaluate(metric=metric, devset=devset, num_threads=8, display_progress=True, display_table=5)