Conversations
Create a conversation
Creates a new conversation resource.
標頭參數
x-ms-user-identity
string
Opaque per-user identity string used to scope endpoint-scoped data to a specific end user. The caller must have the agents/endpoints/UserIdentityImpersonation/action RBAC permission.
要求本文
metadata
OpenAI.Metadata | null
Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format, and querying for objects via API or the dashboard. Keys are strings with a maximum length of 64 characters. Values are strings with a maximum length of 512 characters.
回覆
The request has succeeded.
主體
id
string
必要
The unique ID of the conversation.
object
enum
必要
The object type, which is always conversation.
metadata
OpenAI.Metadata
必要
Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format, and querying for objects via API or the dashboard. Keys are strings with a maximum length of 64 characters. Values are strings with a maximum length of 512 characters.
created_at
integer
必要
The time at which the conversation was created, measured in seconds since the Unix epoch.
Request
curl https://api.openai.com/v1/conversations \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"metadata": {"topic": "demo"},
"items": [
{
"type": "message",
"role": "user",
"content": "Hello!"
}
]
}'
import OpenAI from "openai";
const client = new OpenAI();
const conversation = await client.conversations.create({
metadata: { topic: "demo" },
items: [
{ type: "message", role: "user", content: "Hello!" }
],
});
console.log(conversation);
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ.get("OPENAI_API_KEY"), # This is the default and can be omitted
)
conversation = client.conversations.create()
print(conversation.id)
using System;
using System.Collections.Generic;
using OpenAI.Conversations;
OpenAIConversationClient client = new(
apiKey: Environment.GetEnvironmentVariable("OPENAI_API_KEY")
);
Conversation conversation = client.CreateConversation(
new CreateConversationOptions
{
Metadata = new Dictionary<string, string>
{
{ "topic", "demo" }
},
Items =
{
new ConversationMessageInput
{
Role = "user",
Content = "Hello!",
}
}
}
);
Console.WriteLine(conversation.Id);
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env['OPENAI_API_KEY'], // This is the default and can be omitted
});
const conversation = await client.conversations.create();
console.log(conversation.id);
package main
import (
"context"
"fmt"
"github.com/openai/openai-go"
"github.com/openai/openai-go/conversations"
"github.com/openai/openai-go/option"
)
func main() {
client := openai.NewClient(
option.WithAPIKey("My API Key"),
)
conversation, err := client.Conversations.New(context.TODO(), conversations.ConversationNewParams{})
if err != nil {
panic(err.Error())
}
fmt.Printf("%+v
", conversation.ID)
}
package com.openai.example;
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.conversations.Conversation;
import com.openai.models.conversations.ConversationCreateParams;
public final class Main {
private Main() {}
public static void main(String[] args) {
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
Conversation conversation = client.conversations().create();
}
}
require "openai"
openai = OpenAI::Client.new(api_key: "My API Key")
conversation = openai.conversations.create
puts(conversation)
回應
{
"id": "conv_123",
"object": "conversation",
"created_at": 1741900000,
"metadata": {"topic": "demo"}
}