Skip to content

Commit

Permalink
feat: add ability to chat with already embedded documents
Browse files Browse the repository at this point in the history
  • Loading branch information
shxntanu committed Dec 22, 2024
1 parent 4917061 commit d12568b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 37 deletions.
45 changes: 26 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,29 @@

<div align="center">

**_lesa_**
`[lee - saa]`**Old Norse** <br/>
(v.) to read, to study, to learn
[![Python](https://img.shields.io/badge/python-3.10%2B-blue)](https://www.python.org/downloads/)
![PyPI - Version](https://img.shields.io/pypi/v/lesa)
![PyPI Downloads](https://static.pepy.tech/badge/lesa)

</div>

`lesa` is a CLI tool built in Python that allows you to converse with your documents from the terminal, completely offline and on-device using **Ollama**. Open the terminal in the directory of your choice and start a conversation with any document!

<div align="center">

[![Python](https://img.shields.io/badge/python-3.10%2B-blue)](https://www.python.org/downloads/)
![PyPI - Version](https://img.shields.io/pypi/v/lesa)
![PyPI Downloads](https://static.pepy.tech/badge/lesa)
**_lesa_**
`[lee - saa]`**Old Norse** <br/>
(v.) to read, to study, to learn

<!-- <div align="center">
<sub>Prepared by <a href="https://github.com/shxntanu">Shantanu Wable</a> and <a href="https://github.com/omkargwagholikar">Omkar Wagholikar</a> </sub>
</div> -->

</div>

`lesa` is a CLI tool built in Python that allows you to converse with your documents from the terminal, completely offline and on-device using **Ollama**. Open the terminal in the directory of your choice and start a conversation with any document!




## Usage

To start a conversation with a document (`.pdf` and `.docx` for now), simply run:
Expand All @@ -26,7 +33,13 @@ To start a conversation with a document (`.pdf` and `.docx` for now), simply run
lesa read path/to/your/document
```

### Embed
Or start a conversation with an already-embedded directory, run:

```bash
lesa chat
```

### Embed

To embed all files from your current working directory, run:

Expand All @@ -46,21 +59,15 @@ This creates a `.lesa` config folder in your current working directory that stor

### Prerequisites

This project uses [Ollama](https://ollama.com/) under the hood to utilize the power of large language models. To install Ollama, run:
`lesa` uses [Ollama](https://ollama.com/) under the hood to utilize the power of large language models. To install Ollama, run:

```bash
curl -fsSL https://ollama.com/install.sh | sh
```

This project uses the Llama 3.1 8b or Qwen 4b model as the default. You can use any other model as well, just make sure it has enough context window to understand the content of your documents.

Pull Llama or Qwen using:

```bash
ollama pull qwen:4b
```
`lesa` uses the Llama 3.1 8b as the default. You can use any other model as well, just make sure it has enough context window to understand the content of your documents.

or
Pull Llama using:

```bash
ollama pull llama3.1
Expand All @@ -86,4 +93,4 @@ We welcome contributions! If you'd like to improve `lesa` or have any feedback,

## License

Apache-2.0
Apache-2.0
13 changes: 13 additions & 0 deletions lesa/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,19 @@ def read(file_path: str = typer.Argument(..., help="Path of the file to read")):

return cm.embed_single_document_and_chat(file_path)

@app.command()
def chat():
"""
Starts a chat with the embedded documents.
"""

if not OllamaManager.is_server_running():
console.print(f"[red]Error: Ollama server is not running![/red]")
console.print(f"Start the ollama server by running [cyan]lesa start[/cyan] command.")
raise typer.Exit(1)

return cm.start_conversation()

@app.command()
def use(model_name: str = typer.Argument(..., help="Name of the model to use from Ollama.")):
"""
Expand Down
31 changes: 14 additions & 17 deletions lesa/core/conversations.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,6 @@ def _setup_conversation_pipeline(self, document_model: str):
"""Set up the conversation pipeline connections."""

pass

def embed_documents(self, documents_path: str = None):
"""
Embed documents from a specified path.
:param documents_path: Path to documents. If None, uses the default output_dir from initialization.
"""
pass

def embed_document(self, document_path: str = None):
"""
Embed documents from a specified path.
:param documents_path: Path to documents. If None, uses the default output_dir from initialization.
"""
pass

def _chat(self, chain, system_prompt: Optional[str] = None):
"""
Expand Down Expand Up @@ -157,4 +141,17 @@ def start_conversation(self):
"""
Start a conversation with the RAG pipeline.
"""
self._chat(self.pipe)

llm = self.ollama_manager.serve_llm()
prompt = ChatPromptTemplate([
("system", """Answer any use questions based solely on the context below:
<context>
{context}
</context>"""),
("human", """{input}"""),
])

combine_docs_chain = create_stuff_documents_chain(llm=llm, prompt=prompt)
qa_chain = create_retrieval_chain(retriever=self.embeddings_manager.vector_store.as_retriever(), combine_docs_chain=combine_docs_chain)
return self._chat(qa_chain)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "lesa"
version = "0.1.0.4"
version = "0.1.0.5"
description = "A CLI tool to converse with any document locally using Ollama."
authors = ["Shantanu Wable <shantanuwable2003@gmail.com>", "Omkar Wagholikar <omkarrwagholikar@gmail.com>"]
repository = "https://github.com/shxntanu/lesa"
Expand Down

0 comments on commit d12568b

Please sign in to comment.