链载Ai

标题: 开源AI助手Phidata = LLM 记忆 知识 工具 [打印本页]

作者: 链载Ai    时间: 昨天 10:06
标题: 开源AI助手Phidata = LLM 记忆 知识 工具

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);line-height: 2em;visibility: visible;">Phidata是一个为大型语言模型(LLM)增加记忆、知识和工具的框架。它旨在解决LLM在上下文理解和行动能力方面的局限性。为大语言模型增加记忆、知识和工具。

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;text-wrap: wrap;background-color: rgb(255, 255, 255);visibility: visible;">AI 助手 == LLM + 记忆 + 知识 + 工具 == LLM + Phidata

项目概述

Phidata的设计不仅提升了LLM的功能,还通过提供如PDF AI、ArXiv AI和HackerNews AI等示例应用程序,展示了其在实际应用中的广泛可能性,这些应用可以执行从总结PDF文档到回答专业论文问题、以及概括新闻故事等多种任务。

安装与配置

前提条件

在开始安装和配置Phidata之前,确保系统中已安装以下必要的软件和库:

安装步骤

克隆仓库

  1. 克隆Phidata的GitHub仓库到本地环境:

    git clone [仓库地址]
  2. 进入仓库目录:

    cd phidata

安装依赖

  1. 安装Phidata及其依赖:

    pip install -U phidata
  2. 安装额外的库,例如用于Web搜索的 duckduckgo-search 或财务数据查询的 yfinance

    pip install openai duckduckgo-search
    pip install yfinance

环境设置

  1. 配置环境变量,例如OPENAI的API密钥:

    export OPENAI_API_KEY=sk-xxxx

配置说明

Phidata可能需要配置一些环境变量或修改配置文件以正确运行。这包括:

运行项目

示例1:创建可以搜索网络的助手

  1. 创建一个名为 assistant.py 的文件:

    from phi.assistant import Assistant
    from phi.tools.duckduckgo import DuckDuckGo

    assistant = Assistant(tools=[DuckDuckGo()], show_tool_calls=True)
    assistant.print_response("What's happening in France?", markdown=True)
  2. 运行助手脚本:

    python assistant.py

示例2:创建可以查询财务数据的助手

  1. 创建一个名为 finance_assistant.py 的文件:

    from phi.assistant import Assistant
    from phi.llm.openai import OpenAIChat
    from phi.tools.yfinance import YFinanceTools

    assistant = Assistant(
    llm=OpenAIChat(model="gpt-4o"),
    tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True, company_news=True)],
    show_tool_calls=True,
    markdown=True
    )
    assistant.print_response("What is the stock price of NVDA")
    assistant.print_response("Write a comparison between NVDA and AMD, use all tools available.")
  2. 运行助手脚本:

    python finance_assistant.py

通过遵循以上步骤,用户可以快速搭建并运行自己的Phidata AI助手,以实现特定的业务功能。

使用示例

LLM OS:使用LLM作为新型操作系统的CPU

通过Phidata,LLM可以作为操作系统的核心处理单元,处理复杂的任务和命令,实现一个以智能助手为中心的操作环境。

自主RAG(Retrieval-Augmented Generation)

Phidata使LLM能够自主检索其知识库、网络或聊天历史,以增强生成内容的相关性和准确性。

本地RAG

使用Llama3和Ollama在完全本地的环境中实现RAG,结合PgVector进行高效的数据检索和管理。

投资研究员

利用Llama3和Groq生成股票投资报告,提供深入的市场分析和投资建议。

新闻文章编写

使用Llama3和Groq技术,自动撰写新闻文章,覆盖多种主题和事件。

视频摘要

Phidata结合YouTube和Llama3实现视频内容的自动摘要,帮助用户快速获取视频主要信息。

研究助理

使用Llama3和Groq为学术研究和报告提供撰写支持,增强内容的深度和广度。

Python编程助手

Phidata允许创建可以编写并运行Python代码的助手,以执行数据分析和自动化任务。

示例代码

创建一个名为 python_assistant.py 的文件,该文件配置如下:

from phi.assistant.python import PythonAssistant
from phi.file.local.csv import CsvFile

python_assistant = PythonAssistant(
files=[
CsvFile(
path="https://phidata-public.s3.amazonaws.com/demo_data/IMDB-Movie-Data.csv",
description="Contains information about movies from IMDB."
)
],
pip_install=True,
show_tool_calls=True
)

python_assistant.print_response("What is the average rating of movies?", markdown=True)

运行助手,首先安装必要的库:

pip install pandas
python python_assistant.py

SQL数据分析助手

Phidata通过DuckDbAssistant使LLM能够使用SQL进行数据分析。

示例代码

创建一个名为 data_assistant.py 的文件,配置如下:

import json
from phi.assistant.duckdb import DuckDbAssistant

duckdb_assistant = DuckDbAssistant(
semantic_model=json.dumps({
"tables": [
{
"name": "movies",
"description": "Contains information about movies from IMDB.",
"path": "https://phidata-public.s3.amazonaws.com/demo_data/IMDB-Movie-Data.csv"
}
]
})
)

duckdb_assistant.print_response("What is the average rating of movies? Show me the SQL.", markdown=True)

安装DuckDb并运行数据助手:

pip install duckdb
python data_assistant.py

电影剧本生成助手

通过Phidata创建一个可以生成电影剧本的助手,使用结构化数据(如pydantic模型)从文本中提取特征。

示例代码

创建一个名为 movie_assistant.py 的文件,配置如下:

from typing import List
from pydantic import BaseModel, Field
from rich.pretty import pprint
from phi.assistant import Assistant

class MovieScript(BaseModel):
setting: str = Field(..., description="rovide a nice setting for a blockbuster movie.")
ending: str = Field(..., description="Ending of the movie. If not available, provide a happy ending.")
genre: str = Field(..., description="Genre of the movie. If not available, select action, thriller or romantic comedy.")
name: str = Field(..., description="Give a name to this movie")
characters: List[str] = Field(..., description="Name of characters for this movie.")
storyline: str = Field(..., description="3 sentence storyline for the movie. Make it exciting!")

movie_assistant = Assistant(
description="You help write movie scripts.",
output_model=MovieScript
)

pprint(movie_assistant.run("New York"))

运行 movie_assistant.py 生成电影剧本,输出如下:

MovieScript(
│ setting='A bustling and vibrant New York City',
│ ending='The protagonist saves the city and reconciles with their estranged family.',
│ genre='action',
│ name='City Pulse',
│ characters=['Alex Mercer', 'Nina Castillo', 'Detective Mike Johnson'],
│ storyline='In the heart of New York City, a former cop turned vigilante, Alex Mercer, teams up with a street-smart activist, Nina Castillo, to take down a corrupt political figure who threatens to destroy the city. As they navigate through the intricate web of power and deception, they uncover shocking truths that push them to the brink of their abilities. With time running out, they must race against the clock to save New York and confront their own demons.'
)

这些使用实例展示了Phidata如何通过增加记忆、知识和工具来扩展LLM的功能,使其在各种情景中提供高效、精准的服务。






欢迎光临 链载Ai (https://www.lianzai.com/) Powered by Discuz! X3.5