Skip to content

Commit

Permalink
docs: add the Steaming Response section
Browse files Browse the repository at this point in the history
  • Loading branch information
adcentury committed Sep 13, 2023
1 parent 1cd919d commit 336b27b
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions docs/en/src/guide/functions/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,57 @@ Every function's exports should have a return value, and this value will be retu
Circular references in the return value should be avoided, otherwise it will cause output errors.
:::

### Object Response {#object-response}

The return value can be an object, and the object will be converted to JSON and returned as the Response Body.

```js
module.exports = async function(params, context) {
return {
message: 'Hi, AirCode.',
};
}
```

The response body will be:

```json
{
"message": "Hi, AirCode."
}
```

### Steaming Response {#streaming-response}

You can return any readable stream as the response body, such as a file stream, a response from AI services, etc. The client can consume the stream in real time.

Here is an example of returning the response from OpenAI as a stream:

```js
const OpenAI = require('openai');
const { OpenAIStream } = require('ai');

module.exports = async function(params, context) {
const { messages } = params;

const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
// Get the OpenAI response
const response = await openai.chat.completions.create({
model: 'gpt-4',
stream: true,
messages,
});

// Transform the response into a readable stream
const stream = OpenAIStream(response);

// Return the stream as response, which can be consumed by the client
return stream;
}
```

## Handle Async Tasks {#handle-async-tasks}

Because the exported function is `async`, we recommend using `await` to handle async tasks such as HTTP requests, `Promise` tasks, etc.
Expand Down

0 comments on commit 336b27b

Please sign in to comment.