Skip to content

Commit 6be9992

Browse files
authored
Merge pull request #620 from parterburn/paul/conversations
Add Conversations API
2 parents 9d049a9 + 5e9b269 commit 6be9992

24 files changed

+1958
-0
lines changed

README.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ Stream GPT-5 chats with the Responses API, initiate Realtime WebRTC conversation
7777
- [Vector Store Files](#vector-store-files)
7878
- [Vector Store File Batches](#vector-store-file-batches)
7979
- [Assistants](#assistants)
80+
- [Conversations](#conversations)
8081
- [Threads and Messages](#threads-and-messages)
8182
- [Runs](#runs)
8283
- [Create and Run](#create-and-run)
@@ -1098,6 +1099,128 @@ client.vector_store_file_batches.cancel(
10981099
)
10991100
```
11001101

1102+
### Conversations
1103+
1104+
The Conversations API enables you to create and manage persistent conversations with your models. This is useful for maintaining conversation state across multiple interactions.
1105+
1106+
**Supported Endpoints:**
1107+
- `POST /v1/conversations` - Create a conversation
1108+
- `GET /v1/conversations/{id}` - Retrieve a conversation
1109+
- `PATCH /v1/conversations/{id}` - Modify a conversation
1110+
- `DELETE /v1/conversations/{id}` - Delete a conversation
1111+
- `POST /v1/conversations/{id}/items` - Create items in a conversation
1112+
- `GET /v1/conversations/{id}/items` - List items in a conversation
1113+
- `GET /v1/conversations/{id}/items/{item_id}` - Get a specific item
1114+
- `DELETE /v1/conversations/{id}/items/{item_id}` - Delete an item
1115+
1116+
#### Creating a Conversation
1117+
1118+
To create a new conversation:
1119+
1120+
```ruby
1121+
response = client.conversations.create(
1122+
parameters: {
1123+
metadata: { purpose: "customer_support" }
1124+
}
1125+
)
1126+
conversation_id = response["id"]
1127+
```
1128+
1129+
#### Retrieving a Conversation
1130+
1131+
To retrieve a specific conversation:
1132+
1133+
```ruby
1134+
conversation = client.conversations.retrieve(id: conversation_id)
1135+
```
1136+
1137+
#### Modifying a Conversation
1138+
1139+
To update a conversation's metadata:
1140+
1141+
```ruby
1142+
response = client.conversations.modify(
1143+
id: conversation_id,
1144+
parameters: {
1145+
metadata: { status: "resolved" }
1146+
}
1147+
)
1148+
```
1149+
1150+
#### Deleting a Conversation
1151+
1152+
To delete a conversation:
1153+
1154+
```ruby
1155+
response = client.conversations.delete(id: conversation_id)
1156+
```
1157+
1158+
#### Managing Items in Conversations
1159+
1160+
You can add, retrieve, and manage items within a conversation.
1161+
1162+
##### Creating Items
1163+
1164+
```ruby
1165+
# Create multiple items at once
1166+
response = client.conversations.create_items(
1167+
conversation_id: conversation_id,
1168+
parameters: {
1169+
items: [
1170+
{
1171+
type: "message",
1172+
role: "user",
1173+
content: [
1174+
{ type: "input_text", text: "Hello!" }
1175+
]
1176+
},
1177+
{
1178+
type: "message",
1179+
role: "assistant",
1180+
content: [
1181+
{ type: "input_text", text: "How are you?" }
1182+
]
1183+
}
1184+
]
1185+
}
1186+
)
1187+
```
1188+
1189+
##### Listing Items
1190+
1191+
```ruby
1192+
# List all items in a conversation
1193+
response = client.conversations.list_items(conversation_id: conversation_id)
1194+
items = response["data"]
1195+
1196+
# With parameters
1197+
response = client.conversations.list_items(
1198+
conversation_id: conversation_id,
1199+
parameters: {
1200+
limit: 10,
1201+
order: "asc"
1202+
}
1203+
)
1204+
```
1205+
1206+
##### Retrieving a Specific Item
1207+
1208+
```ruby
1209+
item = client.conversations.get_item(
1210+
conversation_id: conversation_id,
1211+
item_id: item_id
1212+
)
1213+
```
1214+
1215+
##### Deleting an Item
1216+
1217+
```ruby
1218+
response = client.conversations.delete_item(
1219+
conversation_id: conversation_id,
1220+
item_id: item_id
1221+
)
1222+
```
1223+
11011224
### Assistants
11021225

11031226
Assistants are stateful actors that can have many conversations and use tools to perform tasks (see [Assistant Overview](https://platform.openai.com/docs/assistants/overview)).

lib/openai.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
require_relative "openai/version"
2222
require_relative "openai/batches"
2323
require_relative "openai/usage"
24+
require_relative "openai/conversations"
2425

2526
module OpenAI
2627
class Error < StandardError; end

lib/openai/client.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ def usage
105105
@usage ||= OpenAI::Usage.new(client: self)
106106
end
107107

108+
def conversations
109+
@conversations ||= OpenAI::Conversations.new(client: self)
110+
end
111+
108112
def azure?
109113
@api_type&.to_sym == :azure
110114
end

lib/openai/conversations.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
module OpenAI
2+
class Conversations
3+
def initialize(client:)
4+
@client = client
5+
end
6+
7+
def retrieve(id:)
8+
@client.get(path: "/conversations/#{id}")
9+
end
10+
11+
def create(parameters: {})
12+
@client.json_post(path: "/conversations", parameters: parameters)
13+
end
14+
15+
def modify(id:, parameters: {})
16+
@client.json_post(path: "/conversations/#{id}", parameters: parameters)
17+
end
18+
19+
def delete(id:)
20+
@client.delete(path: "/conversations/#{id}")
21+
end
22+
23+
# Item operations within a conversation
24+
def create_items(conversation_id:, parameters: {})
25+
@client.json_post(path: "/conversations/#{conversation_id}/items", parameters: parameters)
26+
end
27+
28+
def list_items(conversation_id:, parameters: {})
29+
@client.get(path: "/conversations/#{conversation_id}/items", parameters: parameters)
30+
end
31+
32+
def get_item(conversation_id:, item_id:)
33+
@client.get(path: "/conversations/#{conversation_id}/items/#{item_id}")
34+
end
35+
36+
def delete_item(conversation_id:, item_id:)
37+
@client.delete(path: "/conversations/#{conversation_id}/items/#{item_id}")
38+
end
39+
end
40+
end

spec/fixtures/cassettes/conversations_create.yml

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/cassettes/conversations_create_items.yml

Lines changed: 69 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/cassettes/conversations_create_items_setup.yml

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)