Skip to content

Commit

Permalink
Merge pull request #13 from voyage-ai/main
Browse files Browse the repository at this point in the history
Add Voyage Query Engine
  • Loading branch information
logan-markewich committed Nov 22, 2023
2 parents 64cb191 + 12818d3 commit da2d4a9
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
46 changes: 46 additions & 0 deletions llama_hub/llama_packs/voyage_query_engine/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Voyage Query Engine Pack

Create a query engine using GPT4 and [Voyage AI](https://docs.voyageai.com/embeddings/) Embeddings.

## CLI Usage

You can download llamapacks directly using `llamaindex-cli`, which comes installed with the `llama-index` python package:

```bash
llamaindex-cli download-llamapack VoyageQueryEnginePack --download-dir ./voyage_pack
```

You can then inspect the files at `./voyage_pack` and use them as a template for your own project.

## Code Usage

You can download the pack to a the `./voyage_pack` directory:

```python
from llama_index.llama_packs import download_llama_pack

# download and install dependencies
VoyageQueryEnginePack = download_llama_pack(
"VoyageQueryEnginePack", "./voyage_pack"
)

# You can use any llama-hub loader to get documents!
voyage_pack = VoyageQueryEnginePack(documents)
```

From here, you can use the pack, or inspect and modify the pack in `./voyage_pack`.

The `run()` function is a light wrapper around `index.as_query_engine().query()`.

```python
response = voyage_pack.run("What did the author do growing up?", similarity_top_k=2)
```

You can also use modules individually.

```python
# Use the index directly
index = voyage_pack.index
query_engine = index.as_query_engine()
retriver = index.as_retriever()
```
3 changes: 3 additions & 0 deletions llama_hub/llama_packs/voyage_query_engine/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from llama_hub.llama_packs.voyage_query_engine.base import VoyageQueryEnginePack

__all__ = ["VoyageQueryEnginePack"]
29 changes: 29 additions & 0 deletions llama_hub/llama_packs/voyage_query_engine/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from typing import Any, Dict, List
from llama_index import ServiceContext, VectorStoreIndex
from llama_index.llms import OpenAI
from llama_index.embeddings import VoyageEmbedding
from llama_index.llama_pack.base import BaseLlamaPack
from llama_index.schema import Document
import os

class VoyageQueryEnginePack(BaseLlamaPack):
def __init__(self, documents: List[Document]) -> None:

llm = OpenAI(model='gpt-4')
embed_model = VoyageEmbedding(model_name="voyage-01", voyage_api_key=os.environ['VOYAGE_API_KEY'])
service_context = ServiceContext.from_defaults(
llm=llm, embed_model=embed_model
)
self.llm = llm
self.index = VectorStoreIndex.from_documents(
documents, service_context=service_context
)

def get_modules(self) -> Dict[str, Any]:
"""Get modules."""
return {"llm": self.llm, "index": self.index}

def run(self, query_str: str, **kwargs: Any) -> Any:
"""Run the pipeline."""
query_engine = self.index.as_query_engine(**kwargs)
return query_engine.query(query_str)
2 changes: 2 additions & 0 deletions llama_hub/llama_packs/voyage_query_engine/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
openai
voyageai

0 comments on commit da2d4a9

Please sign in to comment.