MA

Typeface

Back to portfolio

06— Portfolio

Python AI chatbot

Python AI chatbot06

I developed a small Python command line chatbot that uses Ollama, LangChain and local AI models to generate automated responses to a user's input.

A key feature of the application is its ability to include documents, images, and web pages into the conversation context. This allows the user to prompt the models with specifics from external sources, enhancing the dialogue.

The application is CLI only at the moment.

While some platforms available on the Internet provide similar context-based chatting only for paid accounts, my application offers this functionality freely via local hardware and models. Overall, the goal is to build an accessible way for me to get the most out of the latest language models through a simple yet powerful command line tool.

It interacts with any of the Ollama supported LLMs available, such as mistral, llama2, codellama, etc.

Here is a piece of code showing some use of the Ollama APIs to extract document or URL content (using the specific langchain_community.document_loaders) so that it will be used later by the RetrievalQA together with the user prompt:

def load_from(self, kind: LoaderType, url_or_file: str, prompt: str,
              url_selector_='', chunk_size_=500, chunk_overlap_=0):
    start_time = time.time()
    documents = self.get_docs(kind, url_or_file, url_selector_=url_selector_)
    assert documents and documents is not None, "Data text is empty or None"
    text_splitter = RecursiveCharacterTextSplitter(
        chunk_size=chunk_size_,
        chunk_overlap=chunk_overlap_,
    )
    chunks = text_splitter.split_documents(documents)
    character_count = sum(len(chunk.page_content) for chunk in chunks)
    LoggerUtility.gen_print_wrap(f'Count of characters in extracted text : {character_count}')
    oembed = OllamaEmbeddings(base_url=self.ollama.base_url, model=self.ollama.model)
    vectorstore = FAISS.from_documents(documents=chunks, embedding=oembed)
    if kind == LoaderType.URL:
        response = OllamaWrapper.load_from_url(self, vectorstore, prompt)
    else:
        response = OllamaWrapper.load_from_file(self, vectorstore, prompt)

Command palette

esc
↑↓ move↵ openesc close