Chat Engine - OpenAI Agent Mode

Get started in 5 lines of code

Load data and build index

from llama_index import VectorStoreIndex, SimpleDirectoryReader, ServiceContext
from llama_index.llms import OpenAI

# Necessary to use the latest OpenAI models that support function calling API
service_context = ServiceContext.from_defaults(llm=OpenAI(model="gpt-3.5-turbo-0613"))
data = SimpleDirectoryReader(input_dir="../data/paul_graham/").load_data()
index = VectorStoreIndex.from_documents(data, service_context=service_context)

Configure chat engine

chat_engine = index.as_chat_engine(chat_mode="openai", verbose=True)

Chat with your data

response = chat_engine.chat("Hi")
print(response)
Hello! How can I assist you today?
response = chat_engine.chat(
    "Use the tool to answer: Who did Paul Graham hand over YC to?"
)
print(response)
=== 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.
========================
Paul Graham handed over Y Combinator (YC) to Sam Altman.

Force chat engine to query the index

NOTE: this is a feature unique to the β€œopenai” chat mode (which uses the OpenAIAgent under the hood).

response = chat_engine.chat(
    "What did Paul Graham do growing up?", function_call="query_engine_tool"
)
=== 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 tried programming on the IBM 1401 computer in his school's basement. He later got a microcomputer and started programming games and a word processor. He initially planned to study philosophy in college but switched to AI. He also started publishing essays online, which became a significant focus for him.
========================
print(response)
Growing up, Paul Graham had a passion for writing and programming. He wrote short stories and explored programming on the IBM 1401 computer in his school's basement. He later acquired a microcomputer and began programming games and a word processor. While initially intending to study philosophy in college, he ultimately changed his focus to artificial intelligence (AI). Additionally, he started publishing essays online, which became a significant part of his pursuits.