Python workshop: Box AI API

Rui Barbosa
Box Developer Blog
Published in
5 min readMar 19, 2024

--

Image By vecstock

In today’s data-rich landscape, extracting meaningful insights from unstructured documents is crucial. With the Box AI API, you get a powerful toolkit to help your teams to tap into the value of their content, deliver summaries, and engage in conversational AI.

In this workshop, we’ll dive deep into the capabilities of the Box AI API and explore how it enables applications to swiftly analyze and comprehend textual content. Whether you’re building chatbots, knowledge management systems, or content-driven applications, you’ll discover how the Box AI API can transform your unstructured data into actionable intelligence.

Please note, the Box AI API is only available in a private beta, and capabilities are subject to change.

You can follow along with this workshop with complete code samples via this GitHub Repo. We are using the Box Platform Next Gen Python SDK.

Let’s get started.

Concepts

The Box AI API has 3 main concepts:

  • Ask: Enables your app to ask questions around some content
  • Text generation: Enables your app to have a conversation with Box AI, building on the previous questions and answers
  • Context: The information Box AI will use to answer questions (can be a document, multiple documents, or a snippet of text)

Asking questions about content

Now, let’s create a method to ask Box AI a question.

def ask(
client: Client, question: str, file_id: str, content: str = None
) -> IntelligenceResponse:
"""Ask a question to the AI"""

if file_id is None:
raise ValueError("file_id must be provided")

mode = IntelligenceMode.SINGLE_ITEM_QA
items = [{"id": file_id, "type": "file"}]

# add content if provided
if content is not None:
items[0]["content"] = content

try:
response = client.intelligence.intelligence_ask(
mode=mode,
prompt=question,
items=items,
)
except BoxAPIError as e:
print(f"Error: {e}")

return response

Next, let’s create an input prompt cycle in our main method, so the user can interact with Box AI:

def main():
...

# Summarize the file
response = ask(
client,
"Summarize document.",
DEMO_FILE,
)
print(f"\nResponse: {response.answer}")

while True:
question = input("\nAsk a question (type 'exit' to quit): ")
if question == "exit":
break
response = ask(
client,
question,
DEMO_FILE,
)
print(f"\nResponse: {response.answer}")

This will result in:

Response: The document outlines the requirements and responsibilities for
participating in scuba diving activities. It states that participants must
be able to swim, be in good physical condition, and release the dive center
from any liabilities. Certified divers are required to follow specific
guidelines such as diving with a buddy, planning their dive responsibly,
carrying necessary equipment, and ending the dive if feeling uncomfortable
or unsafe. The document also emphasizes safety measures in case of distress
while diving. Finally, it includes a section for individuals to agree to
these terms by signing their full name.


Ask a question (type 'exit' to quit): do I need to know how to swim

Response: Yes, you must be able to swim to participate in any in water
activities.

Ask a question (type 'exit' to quit): what are the emergency procedures

Response: Emergency procedures outlined in the document include immediately
ending a dive if feeling uncomfortable with planning abilities or
diving conditions, dropping weight belt and inflating BC for flotation
while signaling for assistance if distressed on the surface, and not
holding breath while scuba diving.

Ask a question (type 'exit' to quit):

As you can see, the user can continue to ask questions about the content. In the example above, we used only one single file, but we can ask about multiple files or even a text snippet for context.

Text Generation

The text generation mode allows you to have a conversation with Box AI, building on the previous questions and answers.

Now, let’s create a method to generate text based on a prompt.

def text_gen(
client: Client,
prompt: str,
file_id: str,
content: str = None,
dialogue_history: IntelligenceDialogueHistory = None,
) -> IntelligenceResponse:
"""Ask a question to the AI"""

if file_id is None:
raise ValueError("file_id must be provided")

items = [{"id": file_id, "type": "file"}]

# add content if provided
if content is not None:
items[0]["content"] = content

try:
response = client.intelligence.intelligence_text_gen(
prompt=prompt,
items=items,
dialogue_history=dialogue_history,
)
except BoxAPIError as e:
print(f"Error: {e}")

return response

Finally, let’s create an input prompt cycle in our main method, so the user can interact with Box AI. Notice that we send back the dialog history so that Box AI can keep the context of the conversation.

def main():
...

# Text gen dialog
dialog_history = []
while True:
question = input(
"\nWhat would you like to talk about? (type 'exit' to quit): "
)
if question == "exit":
break

response = text_gen(
client,
question,
DEMO_FILE,
dialogue_history=dialog_history,
)
print(f"\nResponse: {response.answer}")

dialog_history.append(
IntelligenceDialogueHistory(
prompt=question,
answer=response.answer,
created_at=response.created_at,
)
)

Here is a sample conversation.

What would you like to talk about? (type 'exit' to quit): 
how to learn how to fly

Response: To learn how to fly, you can follow these steps:

1. Research flight schools in your area or online courses for pilot training.
2. Enroll in a reputable flight school or sign up for an online course with
good reviews.
3. Obtain the necessary medical certificate from an aviation medical
examiner.
4. Start ground school to learn about aircraft systems, navigation, weather
patterns, and regulations.
5. Begin flight training with a certified flight instructor to gain
hands-on experience in the cockpit.
6. Study for and pass the written knowledge test and practical flying exam
administered by the Federal Aviation Administration (FAA) or relevant
aviation authority.

Remember that learning to fly requires dedication, time commitment, and
financial investment but can be incredibly rewarding!

What would you like to talk about? (type 'exit' to quit): tell me about
navigation

Response: Navigation is the process of determining and controlling a
craft's position and direction. It involves using various tools, techniques,
and systems to ensure that the craft reaches its intended destination safely.

There are several methods of navigation:

1. **Pilotage**: This method involves visually identifying landmarks or
using maps to navigate.
2. **Dead Reckoning**: Dead reckoning involves calculating current position
based on previously determined positions, taking into account speed, time,
and course changes.
3. **Radio Navigation**: Using radio signals from ground-based navigational
aids such as VOR (VHF Omnidirectional Range) or NDB (Non-Directional Beacon)
4. **Inertial Navigation Systems (INS)**: These use gyroscopes and
accelerometers to continuously calculate position based on initial starting
point.
5. **Global Positioning System (GPS)**: GPS uses satellites to determine
precise location anywhere on Earth.

Modern aircraft often use a combination of these methods for redundancy
and accuracy in navigation.

It's important for pilots to be proficient in all aspects of navigation to
ensure safe travels from takeoff to landing!

What would you like to talk about? (type 'exit' to quit):

Notice that, this time, we didn’t ask anything specific about the document, but Box AI was still able to provide answers.

Then Box AI followed up on the previous question and answered with a detailed response about navigation.

Final thoughts

The Box AI API offers a robust toolkit for developers to leverage the power of natural language processing in their applications. Through its capabilities in question answering and text generation, developers can unlock new ways to extract insights from unstructured documents and engage users in meaningful conversations.

Whether you’re summarizing documents, answering specific questions, or generating text based on prompts, the Box AI API provides you a versatile solution for a wide range of use cases — from building chatbots to knowledge management systems.

Thoughts? Comments? Feedback?

Drop us a line on our community forum.

--

--