Skip to content

Commit

Permalink
add CreateBatchWithChatCompletions, CreateBatchWithEmbeddings method
Browse files Browse the repository at this point in the history
  • Loading branch information
eiixy committed Jul 29, 2024
1 parent 37ae3c0 commit 4f72284
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,90 @@ func (c *Client) CreateBatchWithUploadFile(
})
}

type CreateBatchWithChatCompletionsRequest struct {
CompletionWindow string
Metadata map[string]any
FileName string
ChatCompletions []BatchChatCompletion
}

type BatchChatCompletion struct {
CustomID string
ChatCompletion ChatCompletionRequest
}

// CreateBatchWithChatCompletions — API call to Create batch with chat completions.
func (c *Client) CreateBatchWithChatCompletions(
ctx context.Context,
request CreateBatchWithChatCompletionsRequest,
) (response BatchResponse, err error) {
var file File
var lines = make([]BatchLineItem, len(request.ChatCompletions))
for i, completion := range request.ChatCompletions {
lines[i] = BatchChatCompletionRequest{
CustomID: completion.CustomID,
Body: completion.ChatCompletion,
Method: "POST",
URL: BatchEndpointChatCompletions,

Check warning on line 236 in batch.go

View check run for this annotation

Codecov / codecov/patch

batch.go#L228-L236

Added lines #L228 - L236 were not covered by tests
}
}
file, err = c.UploadBatchFile(ctx, UploadBatchFileRequest{
FileName: request.FileName,
Lines: lines,
})
if err != nil {
return

Check warning on line 244 in batch.go

View check run for this annotation

Codecov / codecov/patch

batch.go#L239-L244

Added lines #L239 - L244 were not covered by tests
}
return c.CreateBatch(ctx, CreateBatchRequest{
InputFileID: file.ID,
Endpoint: BatchEndpointChatCompletions,
CompletionWindow: request.CompletionWindow,
Metadata: request.Metadata,
})

Check warning on line 251 in batch.go

View check run for this annotation

Codecov / codecov/patch

batch.go#L246-L251

Added lines #L246 - L251 were not covered by tests
}

type CreateBatchWithEmbeddingsRequest struct {
CompletionWindow string
Metadata map[string]any
FileName string
Embeddings []BatchEmbedding
}

type BatchEmbedding struct {
CustomID string `json:"custom_id"`
Embedding EmbeddingRequest `json:"body"`
}

// CreateBatchWithEmbeddings — API call to Create batch with embeddings.
func (c *Client) CreateBatchWithEmbeddings(
ctx context.Context,
request CreateBatchWithEmbeddingsRequest,
) (response BatchResponse, err error) {
var file File
var lines = make([]BatchLineItem, len(request.Embeddings))
for i, embedding := range request.Embeddings {
lines[i] = BatchEmbeddingRequest{
CustomID: embedding.CustomID,
Body: embedding.Embedding,
Method: "POST",
URL: BatchEndpointEmbeddings,

Check warning on line 278 in batch.go

View check run for this annotation

Codecov / codecov/patch

batch.go#L270-L278

Added lines #L270 - L278 were not covered by tests
}
}
file, err = c.UploadBatchFile(ctx, UploadBatchFileRequest{
FileName: request.FileName,
Lines: lines,
})
if err != nil {
return

Check warning on line 286 in batch.go

View check run for this annotation

Codecov / codecov/patch

batch.go#L281-L286

Added lines #L281 - L286 were not covered by tests
}
return c.CreateBatch(ctx, CreateBatchRequest{
InputFileID: file.ID,
Endpoint: BatchEndpointEmbeddings,
CompletionWindow: request.CompletionWindow,
Metadata: request.Metadata,
})

Check warning on line 293 in batch.go

View check run for this annotation

Codecov / codecov/patch

batch.go#L288-L293

Added lines #L288 - L293 were not covered by tests
}

// RetrieveBatch — API call to Retrieve batch.
func (c *Client) RetrieveBatch(
ctx context.Context,
Expand Down

0 comments on commit 4f72284

Please sign in to comment.