@@ -34,6 +34,22 @@ const (
3434 promptByNamePath = "/api/public/v2/prompts/"
3535)
3636
37+ // PromptType enumerates supported prompt formats.
38+ type PromptType = string
39+
40+ const (
41+ PromptTypeText PromptType = "text"
42+ PromptTypeChat PromptType = "chat"
43+ )
44+
45+ // ChatMessageType enumerates supported chat message kinds.
46+ type ChatMessageType = string
47+
48+ const (
49+ ChatMessageTypeChatMessage ChatMessageType = "chatmessage"
50+ ChatMessageTypePlaceholder ChatMessageType = "placeholder"
51+ )
52+
3753type ListMetadata struct {
3854 Page int `json:"page"`
3955 Limit int `json:"limit"`
@@ -47,12 +63,15 @@ type ListMetadata struct {
4763// The Role field specifies the message role (e.g., "system", "user", "assistant"),
4864// Type specifies the content type, and Content contains the message text with optional placeholders.
4965type ChatMessageWithPlaceHolder struct {
50- Role string `json:"role"`
51- Type string `json:"type"`
52- Content string `json:"content"`
66+ Role string `json:"role"`
67+ Type ChatMessageType `json:"type"`
68+ Content string `json:"content"`
5369}
5470
5571func (c * ChatMessageWithPlaceHolder ) validate () error {
72+ if c .Type != ChatMessageTypeChatMessage && c .Type != ChatMessageTypePlaceholder {
73+ return fmt .Errorf ("'type' must be either 'chatmessage' or 'placeholder'" )
74+ }
5675 if c .Role == "" {
5776 return errors .New ("'role' is required" )
5877 }
@@ -65,17 +84,17 @@ func (c *ChatMessageWithPlaceHolder) validate() error {
6584// PromptEntry represents a complete prompt template with its configuration and messages.
6685//
6786// A prompt entry contains the prompt name, which can be either a string (when Type is "text")
68- // or an array of chat messages with placeholders (for other types ).
69- // The Type field determines the expected structure of the Prompt field.
87+ // or an array of chat messages with placeholders (when Type is "chat" ).
88+ // The Type field determines the expected structure of the Prompt field and must be either "text" or "chat" .
7089// The Config field can contain model-specific configuration parameters.
7190type PromptEntry struct {
72- Name string `json:"name"`
73- Prompt any `json:"prompt"`
74- Type string `json:"type"`
75- Version int `json:"version,omitempty"`
76- Tags []string `json:"tags,omitempty"`
77- Labels []string `json:"labels,omitempty"`
78- Config any `json:"config,omitempty"`
91+ Name string `json:"name"`
92+ Prompt any `json:"prompt"`
93+ Type PromptType `json:"type"`
94+ Version int `json:"version,omitempty"`
95+ Tags []string `json:"tags,omitempty"`
96+ Labels []string `json:"labels,omitempty"`
97+ Config any `json:"config,omitempty"`
7998}
8099
81100// UnmarshalJSON implements custom JSON unmarshalling for PromptEntry.
@@ -92,21 +111,24 @@ func (p *PromptEntry) UnmarshalJSON(data []byte) error {
92111 }{
93112 Alias : (* Alias )(p ),
94113 }
95-
96114 if err := json .Unmarshal (data , temp ); err != nil {
97115 return err
98116 }
99117
100- if p .Type == "text" {
118+ if p .Type != PromptTypeText && p .Type != PromptTypeChat {
119+ return fmt .Errorf ("'type' must be either 'text' or 'chat'" )
120+ }
121+
122+ if p .Type == PromptTypeText {
101123 var promptStr string
102124 if err := json .Unmarshal (temp .Prompt , & promptStr ); err != nil {
103125 return fmt .Errorf ("failed to unmarshal prompt as string for type 'text': %w" , err )
104126 }
105127 p .Prompt = promptStr
106- } else {
128+ } else { // p.Type == "chat"
107129 var promptMessages []ChatMessageWithPlaceHolder
108130 if err := json .Unmarshal (temp .Prompt , & promptMessages ); err != nil {
109- return fmt .Errorf ("failed to unmarshal prompt as []ChatMessageWithPlaceHolder for type '%s': %w" , p .Type , err )
131+ return fmt .Errorf ("failed to unmarshal prompt as []ChatMessageWithPlaceHolder for type '%s': %w" , string ( p .Type ) , err )
110132 }
111133 p .Prompt = promptMessages
112134 }
@@ -121,24 +143,30 @@ func (p *PromptEntry) validate() error {
121143 if p .Prompt == nil {
122144 return errors .New ("'prompt' cannot be nil" )
123145 }
146+ if p .Type == "" {
147+ return errors .New ("'type' is required" )
148+ }
149+ if p .Type != PromptTypeText && p .Type != PromptTypeChat {
150+ return errors .New ("'type' must be either 'text' or 'chat'" )
151+ }
124152
125153 // Validate based on Type field
126- if p .Type == "text" {
154+ if p .Type == PromptTypeText {
127155 // For text type, prompt should be a string
128156 if str , ok := p .Prompt .(string ); ! ok || str == "" {
129157 return errors .New ("'prompt' must be a non-empty string when type is 'text'" )
130158 }
131- } else {
132- // For other types , prompt should be []ChatMessageWithPlaceHolder
159+ } else { // p.Type == PromptTypeChat
160+ // For chat type , prompt should be []ChatMessageWithPlaceHolder
133161 messages , ok := p .Prompt .([]ChatMessageWithPlaceHolder )
134162 if ! ok {
135- return errors .New ("'prompt' must be []ChatMessageWithPlaceHolder when type is not 'text '" )
163+ return errors .New ("'prompt' must be []ChatMessageWithPlaceHolder when type is 'chat '" )
136164 }
137165 if len (messages ) == 0 {
138166 return errors .New ("'prompt' cannot be empty" )
139167 }
140- for _ , msg := range messages {
141- if err := msg .validate (); err != nil {
168+ for i := range messages {
169+ if err := messages [ i ] .validate (); err != nil {
142170 return fmt .Errorf ("invalid prompts message: %w" , err )
143171 }
144172 }
0 commit comments