This repository was archived by the owner on Oct 10, 2025. It is now read-only.
-
Couldn't load subscription status.
- Fork 33
Add docs for LLM Extension #520
Merged
Merged
Changes from 9 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
64dab00
Work on docs
tgahunia05 4ab7294
work on docs
tgahunia05 6a902ef
work on docs
tgahunia05 cfd34e5
Use similar language to other extension docs
tgahunia05 3458e05
Update fcn signature and region/model related notes
tgahunia05 d35ae0e
NIT
tgahunia05 aea7f46
Update to reflect region specification
tgahunia05 41648be
Provide example with vector ext
tgahunia05 38c78f2
Add result to synergy example
tgahunia05 5d396d4
Update docs with PR feedback
tgahunia05 cc530e4
Update src/content/docs/extensions/llm.mdx
tgahunia05 c916531
Update src/content/docs/extensions/llm.mdx
tgahunia05 6ad5d4c
Update src/content/docs/extensions/llm.mdx
tgahunia05 1453626
Update src/content/docs/extensions/llm.mdx
tgahunia05 781555d
Update src/content/docs/extensions/llm.mdx
tgahunia05 33e6ae3
Update src/content/docs/extensions/llm.mdx
tgahunia05 d6fe678
Update src/content/docs/extensions/llm.mdx
tgahunia05 331dd3e
Apply PR feedback
tgahunia05 cde2178
Retire arg: type form to document syntax
tgahunia05 dd007e0
NIT
tgahunia05 3c72603
Fix incorrect provider in example
tgahunia05 15901d3
Apply PR feedback
tgahunia05 771e9dc
Copyedit
sdht0 0fdf4a9
correct invalid optional param syntax, add env var example
tgahunia05 ba5d1cc
Need to init conn before using it
tgahunia05 e71a2aa
Add one more example where create embedding can be used, update filte…
tgahunia05 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,239 @@ | ||
| --- | ||
| title: "LLM Extension" | ||
| --- | ||
|
|
||
| The **LLM extension** provides an interface to fetch text embeddings from supported providers and their models. | ||
|
|
||
| It can be installed and loaded using the following commands: | ||
|
|
||
| ```sql | ||
| INSTALL llm; | ||
| LOAD llm; | ||
| ``` | ||
|
|
||
| The list of supported providers and tested models is as follows. | ||
|
|
||
| | Provider | Models | | ||
| | --- | --- | | ||
| | `amazon-bedrock` | amazon.titan-embed-text-v1 | | ||
| | `google-vertex` | gemini-embedding-001, text-embedding-005, text-multilingual-embedding-002 | | ||
| | `google-gemini` | gemini-embedding-exp-03-07, text-embedding-004, embedding-001 | | ||
| | `ollama` | nomic-embed-text, all-minilm:l6-v2 | | ||
| | `open-ai` | text-embedding-3-large, text-embedding-3-small, text-embedding-ada-002 | | ||
| | `voyage-ai` | voyage-3-large, voyage-3.5, voyage-3.5-lite, voyage-code-3, voyage-finance-2, voyage-law-2, voyage-code-2 | | ||
|
|
||
| Note: Model names are dynamically validated at runtime. If an unsupported model is specified, the provider's API will return an appropriate error. | ||
|
|
||
| --- | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Configuration | ||
|
|
||
| The LLM extension supports configurable options such as credentials, dimensions, and region where applicable. | ||
|
|
||
| - All credentials are provided through environment variables. | ||
| - Dimensions and region are passed to the `CREATE_EMBEDDING` function call, when supported. | ||
|
|
||
| The following environment variables must be set for the corresponding providers: | ||
|
|
||
| | Provider | Required Environment Variables | | ||
| | --- | --- | | ||
| | `amazon-bedrock` | `AWS_ACCESS_KEY`, `AWS_SECRET_ACCESS_KEY` | | ||
| | `google-vertex` | `GOOGLE_CLOUD_PROJECT_ID`, `GOOGLE_VERTEX_ACCESS_KEY` | | ||
| | `google-gemini` | `GOOGLE_GEMINI_API_KEY` | | ||
| | `open-ai` | `OPENAI_API_KEY` | | ||
| | `voyage-ai` | `VOYAGE_API_KEY` | | ||
|
|
||
| Note that the `ollama` provider requires no environment variables but does require the embedding model to be available at `http://localhost:11434`. | ||
|
|
||
| --- | ||
|
|
||
| ## Embedding Function | ||
|
|
||
| The core feature of the LLM extension is a scalar function that generates text embeddings. | ||
|
|
||
| ### Syntax | ||
|
|
||
| ```sql | ||
| CREATE_EMBEDDING(prompt: STRING, provider: STRING, model: STRING) -> LIST<FLOAT> | ||
tgahunia05 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| ### Overloads | ||
tgahunia05 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```sql | ||
| CREATE_EMBEDDING(prompt: STRING, provider: STRING, model: STRING, dimensions: INT64) -> LIST<FLOAT> | ||
| ``` | ||
|
|
||
| ```sql | ||
| CREATE_EMBEDDING(prompt: STRING, provider: STRING, model: STRING, region: STRING) -> LIST<FLOAT> | ||
| ``` | ||
|
|
||
| ```sql | ||
| CREATE_EMBEDDING(prompt: STRING, provider: STRING, model: STRING, dimensions: INT64, region: STRING) -> LIST<FLOAT> | ||
| ``` | ||
|
|
||
| ### Parameters | ||
|
|
||
| - **prompt**: The input text to embed. | ||
| - **provider**: The embedding provider, e.g., `'open-ai'`, `'ollama'`, `'voyage-ai'`, etc. | ||
| - **model**: The identifier of the embedding model. This may change overtime; reference the provider's API reference before use. | ||
| - **dimensions** *(optional)*: The size of the embedding vector to be returned, if supported by the provider. Must be a positive integer. | ||
| - **region** *(optional)*: A region or endpoint hint, if supported by the provider. | ||
|
|
||
| ### Examples | ||
|
|
||
| ```sql | ||
| CREATE_EMBEDDING("Hello world", "openai", "text-embedding-3-small") | ||
tgahunia05 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| With optional parameters: | ||
tgahunia05 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ```sql | ||
| CREATE_EMBEDDING("Hello world", "openai", "text-embedding-3-small", 512) | ||
| ``` | ||
|
|
||
| ```sql | ||
| CREATE_EMBEDDING("Hello world", "amazon-bedrock", "amazon.titan-embed-text-v1", "us-east-1"); | ||
| ``` | ||
|
|
||
| ```sql | ||
| CREATE_EMBEDDING("Hello world", "google-vertex", "gemini-embedding-001", 256, "us-east1"); | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Providers and Configuration | ||
|
|
||
| ### Region Support | ||
|
|
||
| The following providers support region configuration. The list of regions may not be exhaustive. Please reference the provider's API reference for more information about what regions are supported. | ||
| There is no default region; a region argument must be passed to the function call to use these providers. | ||
|
|
||
| | Provider | Regions | | ||
| | --- | --- | | ||
| | `amazon-bedrock` | us-east-1, us-west-2, ap-southeast-1, ap-northeast-1, eu-central-1 | | ||
| | `google-vertex` | us-central1, us-west1, us-west2, us-west3, us-west4, us-east1, us-east4, us-south1, northamerica-northeast1, northamerica-northeast2, southamerica-east1, southamerica-west1, europe-west2, europe-west1, europe-west4, europe-west6, europe-west3, europe-north1, europe-central2, europe-west8, europe-west9, europe-southwest1, asia-south1, asia-southeast1, asia-southeast2, asia-east2, asia-east1, asia-northeast1, asia-northeast2, australia-southeast1, australia-southeast2, asia-northeast3, me-west1 | | ||
|
|
||
| Note: Regions are dynamically validated at runtime. If an unsupported region is specified, the provider's API will return an error. | ||
tgahunia05 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ### Dimensions Support | ||
|
|
||
| The following providers support dimension configuration. | ||
|
|
||
| | Provider | Models | | ||
| | --- | --- | | ||
| | `google-vertex` | gemini-embedding-001, text-embedding-005, text-multilingual-embedding-002 | | ||
| | `open-ai` | text-embedding-3-large, text-embedding-3-small | | ||
| | `voyage-ai` | voyage-3-large, voyage-3.5, voyage-3.5-lite, voyage-code-3 | | ||
|
|
||
| Note: The list of models that support dimension configuration may change | ||
tgahunia05 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| overtime. The set of valid dimensions is determined by the provider and may also | ||
| change over time. It is highly recommended to check the provider's API reference | ||
| before use. If an unsupported dimension is specified, the provider's API will return an error. | ||
|
|
||
| --- | ||
|
|
||
| ## Synergy with Vector Extension | ||
tgahunia05 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
sdht0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| The following example uses the same dataset as the example in the Vector | ||
| documentation. Here we use the embedding function to fetch embeddings. | ||
tgahunia05 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```python | ||
| # create_embeddings.py | ||
| import pyarrow | ||
| import polars | ||
| import kuzu | ||
|
|
||
| DB_NAME = "" | ||
|
|
||
| # Initialize the database | ||
| db = kuzu.Database(DB_NAME) | ||
| conn = kuzu.Connection(db) | ||
|
|
||
| # Install and load vector extension | ||
| conn.execute("INSTALL vector; LOAD vector;") | ||
| conn.execute("INSTALL llm; LOAD llm;") | ||
|
|
||
|
|
||
tgahunia05 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # Create tables | ||
| conn.execute("CREATE NODE TABLE Book(id SERIAL PRIMARY KEY, title STRING, title_embedding FLOAT[1536], published_year INT64);") | ||
| conn.execute("CREATE NODE TABLE Publisher(name STRING PRIMARY KEY);") | ||
| conn.execute("CREATE REL TABLE PublishedBy(FROM Book TO Publisher);") | ||
|
|
||
| # Sample data | ||
| titles = [ | ||
| "The Quantum World", | ||
| "Chronicles of the Universe", | ||
| "Learning Machines", | ||
| "Echoes of the Past", | ||
| "The Dragon's Call" | ||
| ] | ||
| publishers = ["Harvard University Press", "Independent Publisher", "Pearson", "McGraw-Hill Ryerson", "O'Reilly"] | ||
| published_years = [2004, 2022, 2019, 2010, 2015] | ||
|
|
||
| # Insert sample data - Books with embeddings | ||
| for title, published_year in zip(titles, published_years): | ||
| conn.execute( | ||
| """CREATE (b:Book {title: $title, title_embedding: create_embedding($title, 'open-ai', 'text-embedding-3-small'), published_year: $year});""", | ||
tgahunia05 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| {"title": title, "year": published_year} | ||
| ) | ||
| print(f"Inserted book: {title}") | ||
|
|
||
| # Insert sample data - Publishers | ||
| for publisher in publishers: | ||
| conn.execute( | ||
| """CREATE (p:Publisher {name: $publisher});""", | ||
| {"publisher": publisher} | ||
| ) | ||
| print(f"Inserted publisher: {publisher}") | ||
|
|
||
| # Create relationships between Books and Publishers | ||
| for title, publisher in zip(titles, publishers): | ||
| conn.execute(""" | ||
| MATCH (b:Book {title: $title}) | ||
| MATCH (p:Publisher {name: $publisher}) | ||
| CREATE (b)-[:PublishedBy]->(p); | ||
| """, | ||
| {"title": title, "publisher": publisher} | ||
| ) | ||
| print(f"Created relationship between {title} and {publisher}") | ||
|
|
||
|
|
||
|
|
||
| conn.execute( | ||
| """ | ||
| CALL CREATE_VECTOR_INDEX( | ||
| 'Book', | ||
| 'title_vec_index', | ||
| 'title_embedding' | ||
| ) | ||
| """ | ||
| ) | ||
|
|
||
| result = conn.execute( | ||
| """ | ||
| CALL QUERY_VECTOR_INDEX( | ||
| 'Book', | ||
| 'title_vec_index', | ||
| create_embedding('quantum machine learning', 'open-ai', 'text-embedding-3-small'), | ||
| 2 | ||
| ) | ||
| RETURN node.title ORDER BY distance; | ||
| """ | ||
| ) | ||
| print(result.get_as_pl()) | ||
| ``` | ||
|
|
||
| In the above query, we asked for the 2 nearest neighbors of the query vector "quantum machine learning". | ||
| The result is a list of book titles that are most similar to this concept. | ||
tgahunia05 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ``` | ||
| ┌───────────────────┐ | ||
| │ node.title │ | ||
| │ --- │ | ||
| │ str │ | ||
| ╞═══════════════════╡ | ||
| │ Learning Machines │ | ||
| │ The Quantum World │ | ||
| └───────────────────┘ | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.