min_prob: Any tokens generated with probability below this will be considered uncertain
Imports#
import os
os.environ["SERPER_API_KEY"] = ""
import re
import numpy as np
from langchain.schema import BaseRetriever
from langchain.utilities import GoogleSerperAPIWrapper
from langchain.embeddings import OpenAIEmbeddings
from langchain.chat_models import ChatOpenAI
from langchain.llms import OpenAI
from langchain.schema import Document
Retriever#
class SerperSearchRetriever(BaseRetriever):
    def __init__(self, search):
        self.search = search
    
    def get_relevant_documents(self, query: str):
        return [Document(page_content=self.search.run(query))]
    
    async def aget_relevant_documents(self, query: str):
        raise NotImplemented
        
        
retriever = SerperSearchRetriever(GoogleSerperAPIWrapper())
FLARE Chain#
# We set this so we can see what exactly is going on
import langchain
langchain.verbose = True
from langchain.chains import FlareChain
flare = FlareChain.from_llm(
    ChatOpenAI(temperature=0), 
    retriever=retriever,
    max_generation_len=164,
    min_prob=.3,
)
query = "explain in great detail the difference between the langchain framework and baby agi"
flare.run(query)
> Entering new FlareChain chain...
Current Response: 
Prompt after formatting:
Respond to the user message using any relevant context. If context is provided, you should ground your answer in that context. Once you're done responding return FINISHED.
>>> CONTEXT: 
>>> USER INPUT: explain in great detail the difference between the langchain framework and baby agi
>>> RESPONSE:
rtdocs_stable/api.python.langchain.com/en/stable/modules/chains/examples/flare.html