Agent 例子
官方示例如下:https://developers.llamaindex.ai/python/framework/getting_started/starter_example/
import asyncio
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI
# Define a simple calculator tool
def multiply(a: float, b: float) -> float:
"""Useful for multiplying two numbers."""
return a * b
# Create an agent workflow with our calculator tool
agent = FunctionAgent(
tools=[multiply],
llm=OpenAI(model="gpt-4o-mini"),
system_prompt="You are a helpful assistant that can multiply two numbers.",
)
async def main():
# Run the agent
response = await agent.run("What is 1234 * 4567?")
print(str(response))
# Run the agent
if __name__ == "__main__":
asyncio.run(main())
LlamaIndex 默认使用 OpenAI’s gpt-3.5-turbo
方式1
参考:文档
pip install llama-index-llms-deepseek
from llama_index.llms.deepseek import DeepSeek
# you can also set DEEPSEEK_API_KEY in your environment variables
llm = DeepSeek(model="deepseek-reasoner", api_key="you_api_key")
# You might also want to set deepseek as your default llm
# from llama_index.core import Settings
# Settings.llm = llm
方式2
LlamaIndex 默认使用 OpenAI’s gpt-3.5-turbo 。若您使用的是OpenAI兼容API,可采用OpenAILike LLM类。
参考:文档
pip install llama-index-llms-openai-like
from llama_index.llms.openai_like import OpenAILike
llm = OpenAILike(
model="my model",
api_base="https://hostname.com/v1",
api_key="fake",
context_window=128000,
is_chat_model=True,
is_function_calling_model=False,
)
response = llm.complete("Hello World!")
print(str(response))
RAG 例子
官方示例如下:https://developers.llamaindex.ai/python/framework/getting_started/starter_example/
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI
import asyncio
import os
# Create a RAG tool using LlamaIndex
documents = SimpleDirectoryReader("data").load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
def multiply(a: float, b: float) -> float:
"""Useful for multiplying two numbers."""
return a * b
async def search_documents(query: str) -> str:
"""Useful for answering natural language questions about an personal essay written by Paul Graham."""
response = await query_engine.aquery(query)
return str(response)
# Create an enhanced workflow with both tools
agent = FunctionAgent(
tools=[multiply, search_documents],
llm=OpenAI(model="gpt-4o-mini"),
system_prompt="""You are a helpful assistant that can perform calculations
and search through documents to answer questions.""",
)
# Now we can ask questions about the documents or do calculations
async def main():
response = await agent.run(
"What did the author do in college? Also, what's 7 * 8?"
)
print(response)
# Run the agent
if __name__ == "__main__":
asyncio.run(main())
VectorStoreIndex 默认使用 OpenAIEmbedding嵌入模型text-embedding-ada-002
方式1
pip install llama-index-embeddings-huggingface
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.core import Settings
Settings.embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
方式2
参考文档:连接
pip install llama-index-embeddings-openai-like
from llama_index.embeddings.openai_like import OpenAILikeEmbedding
embedding = OpenAILikeEmbedding(
model_name="my-model-name",
api_base="http://localhost:1234/v1",
api_key="fake",
embed_batch_size=10,
)
也可以使用本地嵌入模型:https://developers.llamaindex.ai/python/framework/getting_started/starter_example_local/
转载自 CSDN-专业IT技术社区
原文链接:https://blog.csdn.net/wu_xianqiang/article/details/171663697



