Join BoxWorks 2024 to discover what's possible with content and AI!

Register now!

Generate text with Box AI

Guides Box AI Generate text with Box AI
Edit this page

Generate text with Box AI

Box AI API is currently a beta feature offered subject to Box’s Main Beta Agreement, and the available capabilities may change. Box AI API is available to all Enterprise Plus customers.

You can use Box AI to generate text based on provided content. For example, you can ask Box AI to generate a template based on the content you read or create in Box Notes. Then you can embed the generated text directly into your document.

Send a request

To send a request, use the POST /2.0/ai/text_gen endpoint.

Make sure you have generated the developer token to authorize your app. See prerequisites for using Box AI for details.

cURL
curl -i -L POST "https://api.box.com/2.0/ai/text_gen" \
     -H "content-type: application/json" \
     -H "authorization: Bearer <ACCESS_TOKEN>" \
     -d '{
          "prompt": "Write a social media post about protein powder.",
          "items": [
         {
            "id": "12345678",
            "type": "file",
            "content": "More information about protein powders"
        },
        ],
          "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"
            }
        ],
          "ai_agent": {
            "type": "ai_agent_text_gen",
            "basic_gen": {
              "model": "azure__openai__gpt_3_5_turbo_16k",
              "system_message": "You are a helpful travel assistant specialized in budget travel",
              "prompt_template": "It is `{current_date}`, and I have $8000 and want to spend a week in Azores. What should I see?",
              "num_tokens_for_completion": 8400,
              "llm_endpoint_params": {
                "type": "openai_params",
                "temperature": 2.0,
                "top_p": 1.0,
                "frequency_penalty": 1.5,
                "presence_penalty": 1.5,
                "stop": "<|im_end|>"
              },
              "embeddings": {
                "model": " openai__text_embedding_ada_002",
                "strategy": {
                  "id": "basic",
                  "num_tokens_per_chunk": 64
                }
              },
              "content_template": "---{content}---"
           }
        }
     }'
TypeScript Gen
await client.ai.createAiTextGen({
  prompt: 'Parapharse the document.s',
  items: [
    new AiTextGenItemsField({
      id: fileToAsk.id,
      type: 'file' as AiTextGenItemsTypeField,
      content:
        'The Earth goes around the sun. Sun rises in the East in the morning.',
    }),
  ],
  dialogueHistory: [
    {
      prompt: 'What does the earth go around?',
      answer: 'The sun',
      createdAt: dateTimeFromString('2021-01-01T00:00:00Z'),
    } satisfies AiDialogueHistory,
    {
      prompt: 'On Earth, where does the sun rise?',
      answer: 'East',
      createdAt: dateTimeFromString('2021-01-01T00:00:00Z'),
    } satisfies AiDialogueHistory,
  ],
} satisfies AiTextGen);
Python Gen
client.ai.create_ai_text_gen(
    "Parapharse the document.s",
    [
        CreateAiTextGenItems(
            id=file_to_ask.id,
            type=CreateAiTextGenItemsTypeField.FILE.value,
            content="The Earth goes around the sun. Sun rises in the East in the morning.",
        )
    ],
    dialogue_history=[
        AiDialogueHistory(
            prompt="What does the earth go around?",
            answer="The sun",
            created_at=date_time_from_string("2021-01-01T00:00:00Z"),
        ),
        AiDialogueHistory(
            prompt="On Earth, where does the sun rise?",
            answer="East",
            created_at=date_time_from_string("2021-01-01T00:00:00Z"),
        ),
    ],
    ai_agent=ai_text_gen_agent_config,
)
.NET Gen
await client.Ai.CreateAiTextGenAsync(requestBody: new AiTextGen(prompt: "Parapharse the document.s", items: Array.AsReadOnly(new [] {new AiTextGenItemsField(id: fileToAsk.Id, type: AiTextGenItemsTypeField.File) { Content = "The Earth goes around the sun. Sun rises in the East in the morning." }})) { DialogueHistory = Array.AsReadOnly(new [] {new AiDialogueHistory() { Prompt = "What does the earth go around?", Answer = "The sun", CreatedAt = Utils.DateTimeFromString(dateTime: "2021-01-01T00:00:00Z") },new AiDialogueHistory() { Prompt = "On Earth, where does the sun rise?", Answer = "East", CreatedAt = Utils.DateTimeFromString(dateTime: "2021-01-01T00:00:00Z") }}) });
Swift Gen (Beta)
try await client.ai.createAiTextGen(requestBody: AiTextGen(prompt: "Parapharse the document.s", items: [AiTextGenItemsField(id: fileToAsk.id, type: AiTextGenItemsTypeField.file, content: "The Earth goes around the sun. Sun rises in the East in the morning.")], dialogueHistory: [AiDialogueHistory(prompt: "What does the earth go around?", answer: "The sun", createdAt: try Utils.Dates.dateTimeFromString(dateTime: "2021-01-01T00:00:00Z")), AiDialogueHistory(prompt: "On Earth, where does the sun rise?", answer: "East", createdAt: try Utils.Dates.dateTimeFromString(dateTime: "2021-01-01T00:00:00Z"))]))
Java
List<BoxAIDialogueEntry> dialogueHistory = new ArrayList<>();
dialogueHistory.add(
        new BoxAIDialogueEntry(
            "Make my email about public APIs sound more professional",
            "Here is the first draft of your professional email about public APIs.",
            BoxDateFormat.parse("2013-05-16T15:26:57-07:00")
        )
    );
BoxAIResponse response = BoxAI.sendAITextGenRequest(
    api,
    "Write an email to a client about the importance of public APIs.",
    Collections.singletonList(new BoxAIItem("123456", BoxAIItem.Type.FILE)),
    dialogueHistory
);
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"
}]
ai_agent = {
    'type': 'ai_agent_text_gen',
    'basic_gen': {
        'model': 'openai__gpt_3_5_turbo_16k'
    }
}
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.",
    ai_agent=ai_agent
)
print(answer)
.NET
BoxAIResponse response = await client.BoxAIManager.SendAITextGenRequestAsync(
    new BoxAITextGenRequest
    {
        Prompt = "What is the name of the file?",
        Items = new List<BoxAITextGenItem>() { new BoxAITextGenItem() { Id = "12345" } },
        DialogueHistory = new List<BoxAIDialogueHistory>()
        {
            new BoxAIDialogueHistory() { Prompt = "What is the name of the file?", Answer = "MyFile", CreatedAt = DateTimeOffset.Parse("2024-05-16T15:26:57-07:00") }
            new BoxAIDialogueHistory() { Prompt = "What is the size of the file?", Answer = "10kb", CreatedAt =  DateTimeOffset.Parse("2024-05-16T15:26:57-07:00") }
        }
    };
);
Node
client.ai.textGen(
    {
        prompt: 'What is the capital of France?',
        items: [
            {
                type: 'file',
                id: '12345'
            }
        ],
        dialogue_history: [
            {
                prompt: 'What is the capital of France?',
                answer: 'Paris',
                created_at: '2021-10-01T00:00:00Z'
            },
            {
                prompt: 'What is the capital of Germany?',
                answer: 'Berlin',
                created_at: '2021-10-01T00:00:00Z'
            }
        ]
    })
    .then(response => {
        /* response -> {
            "answer": "The capital of France is Paris.",
            "created_at": "2021-10-01T00:00:00Z",
            "completion_reason": "done"
        } */
    });

Parameters

To make a call, you must pass the following parameters. Mandatory parameters are in bold.

Note: The items array can have exactly one element.

ParameterDescriptionExample
promptThe request for Box AI to generate or refine the text. The prompt's length cannot exceed 10000 characters.Create a meeting agenda for a weekly sales meeting.
items.idBox file ID of the document.1233039227512
items.typeThe type of the supplied input.file
items.contentThe content of the item, often the text representation.This article is about Box AI.
dialogue_history.promptThe prompt previously provided by the client and answered by the Large Language Model (LLM).Make my email about public APIs sound more professional
dialogue_history.answerThe answer previously provided by the LLM.Here is a draft of your professional email about public APIs.
dialogue_history.created_atThe ISO date formatted timestamp of when the previous answer to the prompt was created.2012-12-12T10:53:43-08:00
ai_agentThe AI agent used to override the default agent configuration. This parameter allows you to, for example, replace the default LLM with a custom one using the model parameter, tweak the base prompt to allow for a more customized user experience, or change an LLM parameter, such as temperature, to make the results more or less creative. Before you use the ai_agent parameter, you can get the default configuration using the GET 2.0/ai_agent_default request. For specific use cases, see the AI model overrides tutorial