在今天的科技领域,生成式人工智能(Generative AI)无疑是其中最耀眼的明星之一。从自动化写作到复杂的数据分析,生成式AI正逐渐改变我们的生活方式。而在这一领域,一款名为Phoenix的开源工具正在崭露头角。通过这篇文章,我们将深入探讨Phoenix的创新性和技术特点,以及如何利用它来追踪、评估和迭代生成式AI应用。
什么是Phoenix?
Phoenix是一款开源的追踪和评估工具,旨在帮助开发者追踪、评估和迭代生成式AI应用。其最大的特点是在本地运行,无需复杂的配置即可实现开箱即用的追踪功能。这意味着您可以轻松地对您的AI应用进行追踪、评估和迭代,无论是OpenAI调用、LangChain还是Llama Index,所有的操作都变得异常简便。
如何开始使用Phoenix
为了更好地展示Phoenix的功能,以下是一个简单的安装和使用指南。
第一步:安装Phoenix
首先,我们需要使用pip来安装Phoenix及其相关依赖包,包括OpenAI和LangChain等。在您的终端中输入以下命令:
pip install 'arize-phoenix[evals]' openai langchain
第二步:启动服务器
接下来,我们需要在本地启动Phoenix服务器。创建一个名为server.py
的文件,并在其中输入以下代码:
import phoenix as px
session = px.launch_app()
然后,在终端中运行该文件:
python server.py
此时,您应该会看到服务器正在运行,可以通过提供的URL访问Phoenix仪表盘。
第三步:集成OpenAI应用
现在,我们将展示如何将Phoenix集成到OpenAI应用中。创建一个名为app.py
的文件,并输入以下代码:
此时,您会在Phoenix仪表盘中看到该对话的详细追踪信息,包括输入消息和模型响应。
深入了解LangChain和Llama Index的集成
LangChain集成
LangChain是一个强大的AI工具链,可以帮助开发者快速构建和部署复杂的AI应用。以下是如何将Phoenix集成到LangChain应用中的示例:
import phoenix as px
from phoenix.trace.langchain import LangChainInstrumentor
# To view traces in Phoenix, you will first have to start a Phoenix server. You can do this by running the following:
session = px.launch_app()
# Initialize Langchain auto-instrumentation
LangChainInstrumentor().instrument()
# Initialize your LangChain application
# This might vary on your use-case. An example Chain is shown below
import bs4
from langchain import hub
from langchain_community.document_loaders import WebBaseLoader
from langchain_chroma import Chroma
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-3.5-turbo-0125")
# Load, chunk and index the contents of the blog.
loader = WebBaseLoader(
web_paths=("https://lilianweng.github.io/posts/2023-06-23-agent/",),
bs_kwargs=dict(
parse_only=bs4.SoupStrainer(
class_=("post-content", "post-title", "post-header")
)
),
)
docs = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
splits = text_splitter.split_documents(docs)
vectorstore = Chroma.from_documents(documents=splits, embedding=OpenAIEmbeddings())
# Retrieve and generate using the relevant snippets of the blog.
retriever = vectorstore.as_retriever()
prompt = hub.pull("rlm/rag-prompt")
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
rag_chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
# Execute the chain
response = rag_chain.invoke("What is Task Decomposition?")
在终端中运行该文件,您会在Phoenix仪表盘中看到完整的追踪信息。
Llama Index集成
Llama Index是另一个常用的AI工具,用于数据索引和查询。以下是如何将Phoenix集成到Llama Index应用中的示例:
import phoenix as px
from openinference.instrumentation.llama_index import LlamaIndexInstrumentor
# To view traces in Phoenix, you will first have to start a Phoenix server. You can do this by running the following:
session = px.launch_app()
# Initialize LlamaIndex auto-instrumentation
LlamaIndexInstrumentor().instrument()
import os
from gcsfs import GCSFileSystem
from llama_index.core import (
Settings,
VectorStoreIndex,
StorageContext,
set_global_handler,
load_index_from_storage
)
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.llms.openai import OpenAI
os.environ["OPENAI_API_KEY"] = ""
# LlamaIndex application initialization may vary
# depending on your application
Settings.llm = OpenAI(model="gpt-4-turbo-preview")
Settings.embed_model = OpenAIEmbedding(model="text-embedding-ada-002")
# Load your data and create an index. Here we've provided an example of our documentation
file_system = GCSFileSystem(project="public-assets-275721")
index_path = "arize-phoenix-assets/datasets/unstructured/llm/llama-index/arize-docs/index/"
storage_context = StorageContext.from_defaults(
fs=file_system,
persist_dir=index_path,
)
index = load_index_from_storage(storage_context)
query_engine = index.as_query_engine()
# Query your LlamaIndex application
query_engine.query("What is the meaning of life?")
query_engine.query("How can I deploy Arize?")
# View the traces in the Phoenix UI
px.active_session().url
同样,在终端中运行该文件,您会在Phoenix仪表盘中看到详细的追踪信息。
评估和监控生成式AI应用[]
Phoenix不仅支持追踪功能,还提供了强大的评估和监控工具。通过这些工具,您可以评估模型响应的准确性、检测幻觉(hallucinations)、分析数据的相关性以及监控整个AI应用的性能。
评估幻觉
幻觉是生成式AI模型常见的问题,即模型生成了不符合事实的内容。通过Phoenix,您可以轻松检测并评估模型的幻觉情况:
from phoenix.evals import OpenAIModel, HallucinationEvaluator, QAEvaluator
from phoenix.evals import run_evals
import nest_asyncio
nest_asyncio.apply() # This is needed for concurrency in notebook environments
# Set your OpenAI API key
api_key = "your-api-key" # Replace with your actual API key
eval_model = OpenAIModel(model="gpt-4-turbo-preview", api_key=api_key)
# Define your evaluators
hallucination_evaluator = HallucinationEvaluator(eval_model)
qa_evaluator = QAEvaluator(eval_model)
# Run the evaluations
# Assume 'queries_df' is your input dataframe for Q&A correctness
# and 'spans_df' is your input dataframe for hallucinations
hallucination_eval_df, qa_eval_df = run_evals(
dataframe=queries_df,
evaluators=[hallucination_evaluator, qa_evaluator],
provide_explanation=True
)
# Log the evaluations
from phoenix.trace import SpanEvaluations
px.Client().log_evaluations(
SpanEvaluations(eval_name="Hallucination", dataframe=hallucination_eval_df),
SpanEvaluations(eval_name="QA Correctness", dataframe=qa_eval_df)
)
监控应用性能
通过Phoenix仪表盘,您可以实时监控生成式AI应用的性能,包括响应时间、错误率和其他关键指标。这对于快速识别和解决问题至关重要。
总结
Phoenix是一款功能强大、易于使用的开源工具,能够大大简化生成式AI应用的开发和维护过程。无论是初学者还是经验丰富的开发者,都可以从中受益。通过本文的介绍,相信您已经对Phoenix解,希望您能尝试使用这款工具,提升您的AI开发体验。
关注我,每天带你开发一个AI应用。每周二四六直播,欢迎大家多多交流。