Skip to content

Commit

Permalink
Add an experimental support for Realtime API. (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkht authored Oct 27, 2024
1 parent 84b6d1c commit ef6b94a
Show file tree
Hide file tree
Showing 38 changed files with 2,454 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test.ps1
test*.ps1
**/Debug
**/bin
**/obj
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# Changelog
### Unreleased
### Unrelease
- Add an experimental support for Realtime API.
This enables real-time conversations with the AI assitstant. Please refer to this guide.
[Guide: How to use Realtime API](/Guides/How_to_use_Realtime_API.md)
- Update default api version of Azure OpenAI Service to `2024-10-01-preview`
- Outputs Run and RunStep error messages as warnings.
- Fix minor issues.
- Fix an issue where some request URIs contains duplicate slashes.

### 4.7.0
- Add `-Store` & `-MetaData` params for `Request-ChatCompletion`.
Expand Down
194 changes: 194 additions & 0 deletions Docs/Add-RealtimeSessionItem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
---
external help file: PSOpenAI-help.xml
Module Name: PSOpenAI
online version: https://github.com/mkht/PSOpenAI/blob/main/Docs/Add-RealtimeSessionItem.md
schema: 2.0.0
---

# Add-RealtimeSessionItem

## SYNOPSIS
Add a new Item to the Conversation's context, including messages, function calls, and function call responses.

## SYNTAX

```
Add-RealtimeSessionItem
[-Content] <String>
[-EventId <String>]
[-PreviousItemId <String>]
[-ItemId <String>]
[-ItemType <String>]
[-Status <String>]
[-Role <String>]
[-ContentType <String>]
[-ContentTranscript <String>]
[-FunctionCallId <String>]
[-FunctionCallName <String>]
[-FunctionCallArguments <String>]
[-FunctionCallOutput <String>]
[-TriggerResponse]
```

## DESCRIPTION
Add a new Item to the Conversation's context, including messages, function calls, and function call responses. This event can be used both to populate a "history" of the conversation and to add new items mid-stream

## EXAMPLES

### Example 1
```powershell
PS C:\> Add-RealtimeSessionItem 'Hello. Why is the sun so bright?'
```

### Example 2
```powershell
PS C:\> Add-RealtimeSessionItem 'This is a great question!' -Role assistant
```

## PARAMETERS

### -Content
The content of the message.

```yaml
Type: String
Aliases: Message, Text
Required: True
Position: 0
```
### -ContentTranscript
The transcript of the audio, used for `input_audio` content type.

```yaml
Type: String
Required: False
Position: Named
```

### -ContentType
The content type (`input_text`, `input_audio`, `text`).
The default value is `input_text`.

```yaml
Type: String
Accepted values: input_text, input_audio, text, audio
Required: False
Position: Named
Default value: input_text
```

### -EventId
Optional client-generated ID used to identify this event.

```yaml
Type: String
Required: False
Position: Named
```

### -FunctionCallArguments
The arguments of the function call (for `function_call` items).

```yaml
Type: String
Required: False
Position: Named
```

### -FunctionCallId
The ID of the function call (for `function_call` and `function_call_output` items).

```yaml
Type: String
Required: False
Position: Named
```

### -FunctionCallName
The name of the function being called (for `function_call` items).

```yaml
Type: String
Required: False
Position: Named
```

### -FunctionCallOutput
The output of the function call (for `function_call_output` items).

```yaml
Type: String
Required: False
Position: Named
```

### -ItemId
The unique ID of the item, this can be generated by the client to help manage server-side context, but is not required because the server will generate one if not provided.

```yaml
Type: String
Required: False
Position: Named
```

### -ItemType
The type of the item (`message`, `function_call`, `function_call_output`).
The default is `message`.

```yaml
Type: String
Accepted values: message, function_call, function_call_output
Required: False
Position: Named
Default value: message
```

### -PreviousItemId
The ID of the preceding item after which the new item will be inserted. If not set, the new item will be appended to the end of the conversation. If set, it allows an item to be inserted mid-conversation. If the ID cannot be found, an error will be returned and the item will not be added.

```yaml
Type: String
Required: False
Position: Named
```

### -Role
The role of the message sender (`user`, `assistant`, `system`), only applicable for message items.

```yaml
Type: String
Accepted values: user, assistant, system
Required: False
Position: Named
Default value: user
```

### -Status
The status of the item (`completed`, `incomplete`). These have no effect on the conversation.

```yaml
Type: String
Accepted values: completed, in_progress, incomplete
Required: False
Position: Named
```

### -TriggerResponse
If specified, instructs the server to create a response after adding this item.

```yaml
Type: SwitchParameter
Required: False
Position: Named
```

## INPUTS

## OUTPUTS

## NOTES

## RELATED LINKS

[https://platform.openai.com/docs/api-reference/realtime-client-events/conversation/item/create](https://platform.openai.com/docs/api-reference/realtime-client-events/conversation/item/create)
97 changes: 97 additions & 0 deletions Docs/Connect-RealtimeSession.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
---
external help file: PSOpenAI-help.xml
Module Name: PSOpenAI
online version: https://github.com/mkht/PSOpenAI/blob/main/Docs/Connect-RealtimeSession.md
schema: 2.0.0
---

# Connect-RealtimeSession

## SYNOPSIS
Create a new OpenAI realtime conversation session.

## SYNTAX

```
Connect-RealtimeSession
[-Model <String>]
[-ApiType <OpenAIApiType>]
[-ApiBase <Uri>]
[-AuthType <String>]
[-ApiKey <SecureString>]
```

## DESCRIPTION
Create a new realtime conversation session. This technically means connecting to the WebSocket endpoint provided by the OpenAI Realtime API.

Once the session is opened, the connection will continue until it is disconnected from the server side or a Disconnect-RealtimeSession is executed. You always need run Disconnect-RealtimeSession when you are finished conversation.

## EXAMPLES

### Example 1
```powershell
PS C:\> Connect-RealtimeSession -Model 'gpt-4o-realtime-preview-2024-10-01'
```

## PARAMETERS
### -Model
It is recommended that you always specify the model you want to use.

```yaml
Type: String
Required: False
Position: Named
```
### -ApiBase
Specifies an API endpoint URL such like: `https://your-api-endpoint.test/v1`
If not specified, it will use `https://api.openai.com/v1`

```yaml
Type: System.Uri
Required: False
Position: Named
Default value: https://api.openai.com/v1
```

### -ApiKey
Specifies API key for authentication.
The type of data should `[string]` or `[securestring]`.
If not specified, it will try to use `$global:OPENAI_API_KEY` or `$env:OPENAI_API_KEY`

```yaml
Type: Object
Required: False
Position: Named
```

### -ApiType
Specifies API type of use. `OpenAI`(default) or `Azure`

```yaml
Type: OpenAIApiType
Accepted values: OpenAI, Azure
Required: False
Position: Named
Default value: OpenAI
```

### -AuthType
If you wish to use Entra-ID based authentication, specifies as `azure_ad`.

```yaml
Type: String
Accepted values: openai, azure, azure_ad
Required: False
Position: Named
```

## INPUTS

## OUTPUTS

## NOTES

## RELATED LINKS

[https://platform.openai.com/docs/guides/realtime](https://platform.openai.com/docs/guides/realtime)
37 changes: 37 additions & 0 deletions Docs/Disconnect-RealtimeSession.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
external help file: PSOpenAI-help.xml
Module Name: PSOpenAI
online version: https://github.com/mkht/PSOpenAI/blob/main/Docs/Disconnect-RealtimeSession.md
schema: 2.0.0
---

# Disconnect-RealtimeSession

## SYNOPSIS
Close realtime session.

## SYNTAX

```
Disconnect-RealtimeSession
```

## DESCRIPTION
Terminates a connected conversation session. If audio input/output is activated, they are also terminated.

## EXAMPLES

### Example 1
```powershell
PS C:\> Disconnect-RealtimeSession
```

## PARAMETERS

## INPUTS

## OUTPUTS

## NOTES

## RELATED LINKS
Loading

0 comments on commit ef6b94a

Please sign in to comment.