father-bot / chatgpt_telegram_bot

💬 Telegram bot with ChatGPT, Python-based, using OpenAI's API.
https://t.me/chatgpt_karfly_bot
MIT License
5.08k stars 1.77k forks source link

Integrate LTM, like langchain #255

Open albert-carreras opened 1 year ago

albert-carreras commented 1 year ago

First of all thanks for the awesome package, it works great.

I'm trying to add Long Term Memory to the bot with LangChain. This would not only storing lots and lots of context and keeping very long running conversations with GPT, but would also allow adding big docs as context, or even allow it to browse some websites and answer.

I'm struggling quite a bit as I'm not knowlegeable in python, but the implementation would not be complex at all.

Instead of calling the openAI API directly, there's an intermediate service that handles the internal vector DB and appends relevant context.

Something like this would also work for ingesting documents (which is what I'm trying unsuccesfully...):

async def create_chain(prompt, message, filename = 'full.txt'):
    # Initialize OpenAI language model, conversation chain and embeddings
    llm = OpenAI(temperature=0.7)
    conversation = load_qa_chain(OpenAI(), chain_type="stuff")
    embeddings = OpenAIEmbeddings()

    vectorDB = await ingest_docs(filename)
    docsData = vectorDB.similarity_search(message)
    answer = conversation.run(input_documents=docsData, question=prompt)
    return answer

async def ingest_docs(filename = 'full.txt'):
    raw_text = ""
    with open(filename) as f:
      raw_text = " ".join(line.strip() for line in f)  
    return raw_text

    raw_text = loader.load()

    text_splitter = CharacterTextSplitter(
        separator = " ",
        chunk_size = 1000,
        chunk_overlap  = 200,
        length_function = len,
    )

    texts = text_splitter.split_text(raw_texts)
    vectorDB = FAISS.from_texts(texts, embeddings)

    return vectorDB

Just a suggestion, but it would be awesome!

Thanks 🙏

karfly commented 1 year ago

Hi! Nice idea with docs as context. I'll look at it, cuz now I'm not really familiar with LangChain and other