Update OpenAI and Ollama embedding func examples in README

This commit is contained in:
yangdx 2025-11-28 14:41:29 +08:00
parent 6e2946e78a
commit 1d07ff7f60
2 changed files with 52 additions and 42 deletions

View file

@ -407,6 +407,11 @@ LightRAG 需要利用LLM和Embeding模型来完成文档索引和知识库查询
* LightRAG还支持类OpenAI的聊天/嵌入API * LightRAG还支持类OpenAI的聊天/嵌入API
```python ```python
import os
import numpy as np
from lightrag.utils import wrap_embedding_func_with_attrs
from lightrag.llm.openai import openai_complete_if_cache, openai_embed
async def llm_model_func( async def llm_model_func(
prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs
) -> str: ) -> str:
@ -420,8 +425,9 @@ async def llm_model_func(
**kwargs **kwargs
) )
@wrap_embedding_func_with_attrs(embedding_dim=4096, max_token_size=8192)
async def embedding_func(texts: list[str]) -> np.ndarray: async def embedding_func(texts: list[str]) -> np.ndarray:
return await openai_embed( return await openai_embed.func(
texts, texts,
model="solar-embedding-1-large-query", model="solar-embedding-1-large-query",
api_key=os.getenv("UPSTAGE_API_KEY"), api_key=os.getenv("UPSTAGE_API_KEY"),
@ -432,10 +438,7 @@ async def initialize_rag():
rag = LightRAG( rag = LightRAG(
working_dir=WORKING_DIR, working_dir=WORKING_DIR,
llm_model_func=llm_model_func, llm_model_func=llm_model_func,
embedding_func=EmbeddingFunc( embedding_func=embedding_func # 直接传入装饰后的函数
embedding_dim=4096,
func=embedding_func
)
) )
await rag.initialize_storages() await rag.initialize_storages()
@ -478,19 +481,20 @@ rag = LightRAG(
然后您只需要按如下方式设置LightRAG 然后您只需要按如下方式设置LightRAG
```python ```python
import numpy as np
from lightrag.utils import wrap_embedding_func_with_attrs
from lightrag.llm.ollama import ollama_model_complete, ollama_embed
@wrap_embedding_func_with_attrs(embedding_dim=768, max_token_size=8192)
async def embedding_func(texts: list[str]) -> np.ndarray:
return await ollama_embed.func(texts, embed_model="nomic-embed-text")
# 使用Ollama模型初始化LightRAG # 使用Ollama模型初始化LightRAG
rag = LightRAG( rag = LightRAG(
working_dir=WORKING_DIR, working_dir=WORKING_DIR,
llm_model_func=ollama_model_complete, # 使用Ollama模型进行文本生成 llm_model_func=ollama_model_complete, # 使用Ollama模型进行文本生成
llm_model_name='your_model_name', # 您的模型名称 llm_model_name='your_model_name', # 您的模型名称
# 使用Ollama嵌入函数 embedding_func=embedding_func, # 直接传入装饰后的函数
embedding_func=EmbeddingFunc(
embedding_dim=768,
func=lambda texts: ollama_embed(
texts,
embed_model="nomic-embed-text"
)
),
) )
``` ```
@ -529,19 +533,20 @@ ollama create -f Modelfile qwen2m
您可以使用`llm_model_kwargs`参数配置ollama 您可以使用`llm_model_kwargs`参数配置ollama
```python ```python
import numpy as np
from lightrag.utils import wrap_embedding_func_with_attrs
from lightrag.llm.ollama import ollama_model_complete, ollama_embed
@wrap_embedding_func_with_attrs(embedding_dim=768, max_token_size=8192)
async def embedding_func(texts: list[str]) -> np.ndarray:
return await ollama_embed.func(texts, embed_model="nomic-embed-text")
rag = LightRAG( rag = LightRAG(
working_dir=WORKING_DIR, working_dir=WORKING_DIR,
llm_model_func=ollama_model_complete, # 使用Ollama模型进行文本生成 llm_model_func=ollama_model_complete, # 使用Ollama模型进行文本生成
llm_model_name='your_model_name', # 您的模型名称 llm_model_name='your_model_name', # 您的模型名称
llm_model_kwargs={"options": {"num_ctx": 32768}}, llm_model_kwargs={"options": {"num_ctx": 32768}},
# 使用Ollama嵌入函数 embedding_func=embedding_func, # 直接传入装饰后的函数
embedding_func=EmbeddingFunc(
embedding_dim=768,
func=lambda texts: ollama_embed(
texts,
embed_model="nomic-embed-text"
)
),
) )
``` ```

View file

@ -403,6 +403,11 @@ LightRAG requires the utilization of LLM and Embedding models to accomplish docu
* LightRAG also supports Open AI-like chat/embeddings APIs: * LightRAG also supports Open AI-like chat/embeddings APIs:
```python ```python
import os
import numpy as np
from lightrag.utils import wrap_embedding_func_with_attrs
from lightrag.llm.openai import openai_complete_if_cache, openai_embed
async def llm_model_func( async def llm_model_func(
prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs
) -> str: ) -> str:
@ -416,8 +421,9 @@ async def llm_model_func(
**kwargs **kwargs
) )
@wrap_embedding_func_with_attrs(embedding_dim=4096, max_token_size=8192)
async def embedding_func(texts: list[str]) -> np.ndarray: async def embedding_func(texts: list[str]) -> np.ndarray:
return await openai_embed( return await openai_embed.func(
texts, texts,
model="solar-embedding-1-large-query", model="solar-embedding-1-large-query",
api_key=os.getenv("UPSTAGE_API_KEY"), api_key=os.getenv("UPSTAGE_API_KEY"),
@ -428,10 +434,7 @@ async def initialize_rag():
rag = LightRAG( rag = LightRAG(
working_dir=WORKING_DIR, working_dir=WORKING_DIR,
llm_model_func=llm_model_func, llm_model_func=llm_model_func,
embedding_func=EmbeddingFunc( embedding_func=embedding_func # Pass the decorated function directly
embedding_dim=4096,
func=embedding_func
)
) )
await rag.initialize_storages() await rag.initialize_storages()
@ -476,19 +479,20 @@ If you want to use Ollama models, you need to pull model you plan to use and emb
Then you only need to set LightRAG as follows: Then you only need to set LightRAG as follows:
```python ```python
import numpy as np
from lightrag.utils import wrap_embedding_func_with_attrs
from lightrag.llm.ollama import ollama_model_complete, ollama_embed
@wrap_embedding_func_with_attrs(embedding_dim=768, max_token_size=8192)
async def embedding_func(texts: list[str]) -> np.ndarray:
return await ollama_embed.func(texts, embed_model="nomic-embed-text")
# Initialize LightRAG with Ollama model # Initialize LightRAG with Ollama model
rag = LightRAG( rag = LightRAG(
working_dir=WORKING_DIR, working_dir=WORKING_DIR,
llm_model_func=ollama_model_complete, # Use Ollama model for text generation llm_model_func=ollama_model_complete, # Use Ollama model for text generation
llm_model_name='your_model_name', # Your model name llm_model_name='your_model_name', # Your model name
# Use Ollama embedding function embedding_func=embedding_func, # Pass the decorated function directly
embedding_func=EmbeddingFunc(
embedding_dim=768,
func=lambda texts: ollama_embed(
texts,
embed_model="nomic-embed-text"
)
),
) )
``` ```
@ -527,19 +531,20 @@ ollama create -f Modelfile qwen2m
Tiy can use `llm_model_kwargs` param to configure ollama: Tiy can use `llm_model_kwargs` param to configure ollama:
```python ```python
import numpy as np
from lightrag.utils import wrap_embedding_func_with_attrs
from lightrag.llm.ollama import ollama_model_complete, ollama_embed
@wrap_embedding_func_with_attrs(embedding_dim=768, max_token_size=8192)
async def embedding_func(texts: list[str]) -> np.ndarray:
return await ollama_embed.func(texts, embed_model="nomic-embed-text")
rag = LightRAG( rag = LightRAG(
working_dir=WORKING_DIR, working_dir=WORKING_DIR,
llm_model_func=ollama_model_complete, # Use Ollama model for text generation llm_model_func=ollama_model_complete, # Use Ollama model for text generation
llm_model_name='your_model_name', # Your model name llm_model_name='your_model_name', # Your model name
llm_model_kwargs={"options": {"num_ctx": 32768}}, llm_model_kwargs={"options": {"num_ctx": 32768}},
# Use Ollama embedding function embedding_func=embedding_func, # Pass the decorated function directly
embedding_func=EmbeddingFunc(
embedding_dim=768,
func=lambda texts: ollama_embed(
texts,
embed_model="nomic-embed-text"
)
),
) )
``` ```