聊天引擎 - OpenAI 智能体模式¶
如果你在Colab上打开这个Notebook,你可能需要安装LlamaIndex 🦙。
In [ ]:
Copied!
%pip install llama-index-llms-openai
%pip install llama-index-llms-openai
In [ ]:
Copied!
!pip install llama-index
!pip install llama-index
下载数据¶
In [ ]:
Copied!
!mkdir -p 'data/paul_graham/'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'
!mkdir -p 'data/paul_graham/'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'
--2023-11-20 14:52:58-- https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.110.133, 185.199.108.133, 185.199.109.133, ... Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.110.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 75042 (73K) [text/plain] Saving to: ‘data/paul_graham/paul_graham_essay.txt’ data/paul_graham/pa 100%[===================>] 73.28K --.-KB/s in 0.02s 2023-11-20 14:52:58 (2.86 MB/s) - ‘data/paul_graham/paul_graham_essay.txt’ saved [75042/75042]
5行代码快速入门¶
加载数据并构建索引
In [ ]:
Copied!
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.llms.openai import OpenAI
# Necessary to use the latest OpenAI models that support function calling API
llm = OpenAI(model="gpt-3.5-turbo-0613")
data = SimpleDirectoryReader(input_dir="../data/paul_graham/").load_data()
index = VectorStoreIndex.from_documents(data)
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.llms.openai import OpenAI
# 必须使用支持函数调用API的最新OpenAI模型
llm = OpenAI(model="gpt-3.5-turbo-0613")
data = SimpleDirectoryReader(input_dir="../data/paul_graham/").load_data()
index = VectorStoreIndex.from_documents(data)
配置聊天引擎
In [ ]:
Copied!
chat_engine = index.as_chat_engine(chat_mode="openai", llm=llm, verbose=True)
chat_engine = index.as_chat_engine(chat_mode="openai", llm=llm, verbose=True)
与您的数据对话
In [ ]:
Copied!
response = chat_engine.chat("Hi")
print(response)
response = chat_engine.chat("Hi")
print(response)
STARTING TURN 1 --------------- Hello! How can I assist you today?
In [ ]:
Copied!
response = chat_engine.chat(
"Use the tool to answer: Who did Paul Graham hand over YC to?"
)
print(response)
response = chat_engine.chat(
"使用该工具回答:Paul Graham将YC移交给谁了?"
)
print(response)
STARTING TURN 1 --------------- === Calling Function === Calling function: query_engine_tool with args: { "input": "Who did Paul Graham hand over YC to?" } Got output: Paul Graham handed over YC to Sam Altman. ======================== STARTING TURN 2 --------------- Paul Graham handed over Y Combinator (YC) to Sam Altman.
In [ ]:
Copied!
response = chat_engine.stream_chat(
"Use the tool to answer: Who did Paul Graham hand over YC to?"
)
print(response)
response = chat_engine.stream_chat(
"使用该工具回答:Paul Graham将YC移交给谁了?"
)
print(response)
STARTING TURN 1 --------------- === Calling Function === Calling function: query_engine_tool with args: { "input": "Who did Paul Graham hand over YC to?" } Got output: Paul Graham handed over YC to Sam Altman. ======================== STARTING TURN 2 ---------------
强制聊天引擎查询索引¶
注意:这是"openai"聊天模式独有的功能(底层使用的是OpenAIAgent
)。
In [ ]:
Copied!
response = chat_engine.chat(
"What did Paul Graham do growing up?", tool_choice="query_engine_tool"
)
response = chat_engine.chat(
"Paul Graham成长过程中做了什么?", tool_choice="query_engine_tool"
)
STARTING TURN 1 --------------- === Calling Function === Calling function: query_engine_tool with args: { "input": "What did Paul Graham do growing up?" } Got output: Growing up, Paul Graham worked on writing and programming. He wrote short stories and also tried his hand at programming on the IBM 1401 computer that his school district had. He later got a microcomputer, a TRS-80, and started programming more extensively, writing simple games and even a word processor. ======================== STARTING TURN 2 ---------------
In [ ]:
Copied!
print(response)
打印(response)
Growing up, Paul Graham worked on writing and programming. He wrote short stories and also tried his hand at programming on the IBM 1401 computer that his school district had. He later got a microcomputer, a TRS-80, and started programming more extensively, writing simple games and even a word processor.