-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support Box AI features (#877)
- Loading branch information
1 parent
6c45394
commit 3026d2a
Showing
4 changed files
with
269 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
AI | ||
== | ||
|
||
AI allows to send an intelligence request to supported large language models and returns an answer based on the provided prompt and items. | ||
|
||
<!-- START doctoc generated TOC please keep comment here to allow auto update --> | ||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | ||
|
||
- [Send AI request](#send-ai-request) | ||
- [Send AI text generation request](#send-ai-text-generation-request) | ||
|
||
<!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
|
||
Send AI request | ||
------------------------ | ||
|
||
Calling the [`client.send_ai_question(items, prompt, mode)`][send-ai-question] method will send an AI request to the supported large language models. The `items` parameter is a list of items to be processed by the LLM, often files. The `prompt` provided by the client to be answered by the LLM. The prompt's length is limited to 10000 characters. The `mode` specifies if this request is for a single or multiple items. If you select `single_item_qa` the items array can have one element only. Selecting `multiple_item_qa` allows you to provide up to 25 items. | ||
|
||
|
||
|
||
<!-- sample post_ai_ask --> | ||
```python | ||
items = [{ | ||
"id": "1582915952443", | ||
"type": "file", | ||
"content": "More information about public APIs" | ||
}] | ||
answer = client.send_ai_question( | ||
items=items, | ||
prompt="What is this file?", | ||
mode="single_item_qa" | ||
) | ||
print(answer) | ||
``` | ||
|
||
NOTE: The AI endpoint may return a 412 status code if you use for your request a file which has just been updated to the box. | ||
It usually takes a few seconds for the file to be indexed and available for the AI endpoint. | ||
|
||
[send-ai-question]: https://box-python-sdk.readthedocs.io/en/latest/boxsdk.client.html#boxsdk.client.client.Client.send_ai_question | ||
|
||
Send AI text generation request | ||
------------------------ | ||
|
||
Calling the [`client.send_ai_text_gen(dialogue_history, items, prompt)`][send-ai-text-gen] method will send an AI text generation request to the supported large language models. The `dialogue_history` parameter is history of prompts and answers previously passed to the LLM. This provides additional context to the LLM in generating the response. The `items` parameter is a list of items to be processed by the LLM, often files. The `prompt` provided by the client to be answered by the LLM. The prompt's length is limited to 10000 characters. | ||
|
||
<!-- sample post_ai_text_gen --> | ||
```python | ||
items = [{ | ||
"id": "1582915952443", | ||
"type": "file", | ||
"content": "More information about public APIs" | ||
}] | ||
dialogue_history = [{ | ||
"prompt": "Make my email about public APIs sound more professional", | ||
"answer": "Here is the first draft of your professional email about public APIs", | ||
"created_at": "2013-12-12T10:53:43-08:00" | ||
}, | ||
{ | ||
"prompt": "Can you add some more information?", | ||
"answer": "Public API schemas provide necessary information to integrate with APIs...", | ||
"created_at": "2013-12-12T11:20:43-08:00" | ||
}] | ||
answer = client.send_ai_text_gen( | ||
dialogue_history=dialogue_history, | ||
items=items, | ||
prompt="Write an email to a client about the importance of public APIs." | ||
) | ||
print(answer) | ||
``` | ||
|
||
[send-ai-text-gen]: https://box-python-sdk.readthedocs.io/en/latest/boxsdk.client.html#boxsdk.client.client.Client.send_ai_text_gen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from datetime import datetime | ||
|
||
import pytest | ||
|
||
from test.integration_new import CLIENT | ||
from test.integration_new.context_managers.box_test_folder import BoxTestFolder | ||
from test.integration_new.context_managers.box_test_file import BoxTestFile | ||
|
||
FOLDER_TESTS_DIRECTORY_NAME = 'folder-integration-tests' | ||
|
||
|
||
@pytest.fixture(scope='module', autouse=True) | ||
def parent_folder(): | ||
with BoxTestFolder(name=f'{FOLDER_TESTS_DIRECTORY_NAME} {datetime.now()}') as folder: | ||
yield folder | ||
|
||
|
||
def test_send_ai_question(parent_folder, small_file_path): | ||
with BoxTestFile(parent_folder=parent_folder, file_path=small_file_path) as file: | ||
items = [{ | ||
'id': file.id, | ||
'type': 'file', | ||
'content': 'The sun raises in the east.' | ||
}] | ||
answer = CLIENT.send_ai_question( | ||
items=items, | ||
prompt='Which direction does the sun raise?', | ||
mode='single_item_qa' | ||
) | ||
assert 'east' in answer['answer'].lower() | ||
assert answer['completion_reason'] == 'done' | ||
|
||
|
||
def test_send_ai_text_gen(parent_folder, small_file_path): | ||
with BoxTestFile(parent_folder=parent_folder, file_path=small_file_path) as file: | ||
items = [{ | ||
'id': file.id, | ||
'type': 'file', | ||
'content': 'The sun raises in the east.' | ||
}] | ||
dialogue_history = [{ | ||
'prompt': 'How does the sun rise?', | ||
'answer': 'The sun raises in the east.', | ||
'created_at': '2013-12-12T10:53:43-08:00' | ||
}, { | ||
'prompt': 'How many hours does it take for the sun to rise?', | ||
'answer': 'It takes 24 hours for the sun to rise.', | ||
'created_at': '2013-12-12T11:20:43-08:00' | ||
}] | ||
answer = CLIENT.send_ai_text_gen( | ||
dialogue_history=dialogue_history, | ||
items=items, | ||
prompt='Which direction does the sun raise?' | ||
) | ||
assert 'east' in answer['answer'].lower() | ||
assert answer['completion_reason'] == 'done' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters