Skip to content

Commit fe341e9

Browse files
kasarolzzwCoda-bot
andauthored
[feat][prompt] prompt as a service (ptaas) (#21)
ptaas Co-Authored-By: Coda <coda@bytedance.com>
1 parent 71a0bb4 commit fe341e9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1547
-136
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ go.work.sum
2828
/.vscode
2929
/output
3030
coverage.out
31-
/apb
31+
/apb
32+
.coda/

client.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ func GetPrompt(ctx context.Context, param GetPromptParam, options ...GetPromptOp
318318

319319
// PromptFormat format prompt with variables
320320
func PromptFormat(ctx context.Context, prompt *entity.Prompt, variables map[string]any, options ...PromptFormatOption) (
321-
messages []*entity.Message, err error) {
321+
messages []*entity.Message, err error,
322+
) {
322323
return getDefaultClient().PromptFormat(ctx, prompt, variables, options...)
323324
}
324325

@@ -495,6 +496,20 @@ func (c *loopClient) PromptFormat(ctx context.Context, loopPrompt *entity.Prompt
495496
return c.promptProvider.PromptFormat(ctx, loopPrompt, variables, config)
496497
}
497498

499+
func (c *loopClient) Execute(ctx context.Context, req *entity.ExecuteParam, options ...ExecuteOption) (entity.ExecuteResult, error) {
500+
if c.closed {
501+
return entity.ExecuteResult{}, consts.ErrClientClosed
502+
}
503+
return c.promptProvider.Execute(ctx, req, options...)
504+
}
505+
506+
func (c *loopClient) ExecuteStreaming(ctx context.Context, req *entity.ExecuteParam, options ...ExecuteStreamingOption) (entity.StreamReader[entity.ExecuteResult], error) {
507+
if c.closed {
508+
return nil, consts.ErrClientClosed
509+
}
510+
return c.promptProvider.ExecuteStreaming(ctx, req, options...)
511+
}
512+
498513
func (c *loopClient) StartSpan(ctx context.Context, name, spanType string, opts ...StartSpanOption) (context.Context, Span) {
499514
if c.closed {
500515
return ctx, DefaultNoopSpan

entity/prompt.go

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
package entity
55

6-
import "github.com/coze-dev/cozeloop-go/internal/util"
6+
import (
7+
"github.com/coze-dev/cozeloop-go/internal/util"
8+
)
79

810
type Prompt struct {
911
WorkspaceID string `json:"workspace_id"`
@@ -29,9 +31,24 @@ const (
2931
)
3032

3133
type Message struct {
32-
Role Role `json:"role"`
33-
Content *string `json:"content,omitempty"`
34-
Parts []*ContentPart `json:"parts,omitempty"`
34+
Role Role `json:"role"`
35+
ReasoningContent *string `json:"reasoning_content,omitempty"`
36+
Content *string `json:"content,omitempty"`
37+
Parts []*ContentPart `json:"parts,omitempty"`
38+
ToolCallID *string `json:"tool_call_id,omitempty"`
39+
ToolCalls []*ToolCall `json:"tool_calls,omitempty"`
40+
}
41+
42+
type ToolCall struct {
43+
Index int32 `json:"index"`
44+
ID string `json:"id"`
45+
Type ToolType `json:"type"`
46+
FunctionCall *FunctionCall `json:"function_call,omitempty"`
47+
}
48+
49+
type FunctionCall struct {
50+
Name string `json:"name"`
51+
Arguments *string `json:"arguments,omitempty"`
3552
}
3653

3754
type Role string
@@ -45,16 +62,18 @@ const (
4562
)
4663

4764
type ContentPart struct {
48-
Type ContentType `json:"type"`
49-
Text *string `json:"text,omitempty"`
50-
ImageURL *string `json:"image_url,omitempty"`
65+
Type ContentType `json:"type"`
66+
Text *string `json:"text,omitempty"`
67+
ImageURL *string `json:"image_url,omitempty"`
68+
Base64Data *string `json:"base64_data,omitempty"`
5169
}
5270

5371
type ContentType string
5472

5573
const (
5674
ContentTypeText ContentType = "text"
5775
ContentTypeImageURL ContentType = "image_url"
76+
ContentTypeBase64Data ContentType = "base64_data"
5877
ContentTypeMultiPartVariable ContentType = "multi_part_variable"
5978
)
6079

@@ -119,6 +138,25 @@ type LLMConfig struct {
119138
JSONMode *bool `json:"json_mode,omitempty"`
120139
}
121140

141+
type ExecuteParam struct {
142+
PromptKey string `json:"prompt_key"`
143+
Version string `json:"version,omitempty"`
144+
Label string `json:"label,omitempty"`
145+
VariableVals map[string]any `json:"variable_vals,omitempty"`
146+
Messages []*Message `json:"messages,omitempty"`
147+
}
148+
149+
type ExecuteResult struct {
150+
Message *Message `json:"message,omitempty"`
151+
FinishReason *string `json:"finish_reason,omitempty"`
152+
Usage *TokenUsage `json:"usage,omitempty"`
153+
}
154+
155+
type TokenUsage struct {
156+
InputTokens int `json:"input_tokens"`
157+
OutputTokens int `json:"output_tokens"`
158+
}
159+
122160
func (p *Prompt) DeepCopy() *Prompt {
123161
if p == nil {
124162
return nil
@@ -181,12 +219,14 @@ func (cp *ContentPart) DeepCopy() *ContentPart {
181219
return nil
182220
}
183221
copied := &ContentPart{
184-
Type: cp.Type,
185-
ImageURL: cp.ImageURL,
222+
Type: cp.Type,
186223
}
187224
if cp.Text != nil {
188225
copied.Text = util.Ptr(*cp.Text)
189226
}
227+
if cp.ImageURL != nil {
228+
copied.ImageURL = util.Ptr(*cp.ImageURL)
229+
}
190230
return copied
191231
}
192232

entity/stream.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
2+
// SPDX-License-Identifier: MIT
3+
4+
package entity
5+
6+
type StreamReader[T any] interface {
7+
Recv() (T, error)
8+
}

error.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ var (
1616
ErrParsePrivateKey = consts.ErrParsePrivateKey
1717
)
1818

19-
type AuthError = consts.AuthError
20-
type RemoteServiceError = consts.RemoteServiceError
19+
type (
20+
AuthError = consts.AuthError
21+
RemoteServiceError = consts.RemoteServiceError
22+
)

examples/init/log/log.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ func main() {
3131
cozeloop.Close(ctx)
3232
}
3333

34-
type CustomLogger struct {
35-
}
34+
type CustomLogger struct{}
3635

3736
func (l *CustomLogger) CtxDebugf(ctx context.Context, format string, v ...interface{}) {
3837
fmt.Printf("[Custom] [DEBUG] "+format+"\n", v...)

examples/prompt/prompt_hub.go renamed to examples/prompt/prompt_hub/prompt_hub.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ func (r *llmRunner) llmCall(ctx context.Context, messages []*entity.Message) (er
131131
defer span.Finish(ctx)
132132

133133
// llm is processing
134-
//baseURL := "https://xxx"
135-
//ak := "****"
134+
// baseURL := "https://xxx"
135+
// ak := "****"
136136
modelName := "gpt-4o-2024-05-13"
137137
maxTokens := 1000 // range: [0, 4096]
138138
//transport := &MyTransport{

examples/prompt/prompt_hub_jinja/prompt_hub_jinja.go renamed to examples/prompt/prompt_hub/prompt_hub_jinja/prompt_hub_jinja.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ func (r *llmRunner) llmCall(ctx context.Context, messages []*entity.Message) (er
160160
defer span.Finish(ctx)
161161

162162
// llm is processing
163-
//baseURL := "https://xxx"
164-
//ak := "****"
163+
// baseURL := "https://xxx"
164+
// ak := "****"
165165
modelName := "gpt-4o-2024-05-13"
166166
maxTokens := 1000 // range: [0, 4096]
167167
//transport := &MyTransport{

examples/prompt/prompt_hub_label/prompt_hub_label.go renamed to examples/prompt/prompt_hub/prompt_hub_label/prompt_hub_label.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ func (r *llmRunner) llmCall(ctx context.Context, messages []*entity.Message) (er
125125
defer span.Finish(ctx)
126126

127127
// llm is processing
128-
//baseURL := "https://xxx"
129-
//ak := "****"
128+
// baseURL := "https://xxx"
129+
// ak := "****"
130130
modelName := "gpt-4o-2024-05-13"
131131
maxTokens := 1000 // range: [0, 4096]
132132
//transport := &MyTransport{

examples/prompt/prompt_hub_multipart/prompt_hub_multipart.go renamed to examples/prompt/prompt_hub/prompt_hub_multipart/prompt_hub_multipart.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func main() {
7777

7878
// 4.Format messages of the prompt
7979
imageText := "图片样例"
80-
imageURL := "https://example.com" //公网访问地址
80+
imageURL := "https://example.com" // 公网访问地址
8181
messages, err := llmRunner.client.PromptFormat(ctx, prompt, map[string]any{
8282
"num": "2",
8383
"count": 10,
@@ -131,8 +131,8 @@ func (r *llmRunner) llmCall(ctx context.Context, messages []*entity.Message) (er
131131
defer span.Finish(ctx)
132132

133133
// llm is processing
134-
//baseURL := "https://xxx"
135-
//ak := "****"
134+
// baseURL := "https://xxx"
135+
// ak := "****"
136136
modelName := "gpt-4o-2024-05-13"
137137
maxTokens := 1000 // range: [0, 4096]
138138
//transport := &MyTransport{

0 commit comments

Comments
 (0)