|
| 1 | +package apis |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +// PromptVersionResponse is the wrapper for the API response |
| 11 | +type PromptVersionResponse struct { |
| 12 | + Data PromptVersion `json:"data"` |
| 13 | +} |
| 14 | + |
| 15 | +// PromptVersion represents a version of a prompt with its configuration |
| 16 | +type PromptVersion struct { |
| 17 | + ID string `json:"id"` |
| 18 | + Version int `json:"version"` |
| 19 | + Description string `json:"description"` |
| 20 | + PromptID string `json:"promptId"` |
| 21 | + Config PromptConfig `json:"config"` |
| 22 | + CreatedAt string `json:"createdAt"` |
| 23 | + UpdatedAt string `json:"updatedAt"` |
| 24 | + DeletedAt string `json:"deletedAt,omitempty"` |
| 25 | +} |
| 26 | + |
| 27 | +// PromptConfig contains the configuration settings for a prompt version |
| 28 | +type PromptConfig struct { |
| 29 | + Tags map[string]interface{} `json:"tags"` |
| 30 | + Model string `json:"model"` |
| 31 | + Author Author `json:"author"` |
| 32 | + ModelID string `json:"modelId"` |
| 33 | + Messages []Message `json:"messages"` |
| 34 | + Provider string `json:"provider"` |
| 35 | + ModelParameters ModelParameters `json:"modelParameters"` |
| 36 | +} |
| 37 | + |
| 38 | +// Author represents the author information |
| 39 | +type Author struct { |
| 40 | + ID string `json:"id"` |
| 41 | + Name string `json:"name"` |
| 42 | + Email string `json:"email"` |
| 43 | + Image string `json:"image"` |
| 44 | +} |
| 45 | + |
| 46 | +// Message represents a message in the prompt |
| 47 | +type Message struct { |
| 48 | + ID string `json:"id"` |
| 49 | + Index int `json:"index"` |
| 50 | + Payload MessagePayload `json:"payload"` |
| 51 | + CurrentType string `json:"currentType"` |
| 52 | + OriginalType string `json:"originalType"` |
| 53 | +} |
| 54 | + |
| 55 | +// MessagePayload contains the role and content of a message |
| 56 | +type MessagePayload struct { |
| 57 | + Role string `json:"role"` |
| 58 | + Content MessagePayloadContent `json:"content"` |
| 59 | +} |
| 60 | + |
| 61 | +type MessagePayloadContent struct { |
| 62 | + MessagePayloadContentStr *string |
| 63 | + MessagePayloadContentArray []MessagePayloadContentBlock |
| 64 | +} |
| 65 | + |
| 66 | +type MessagePayloadContentBlock struct { |
| 67 | + Type string `json:"type"` |
| 68 | + Text string `json:"text"` |
| 69 | +} |
| 70 | + |
| 71 | +func (m *MessagePayloadContent) UnmarshalJSON(data []byte) error { |
| 72 | + var messageStr string |
| 73 | + if err := json.Unmarshal(data, &messageStr); err == nil { |
| 74 | + m.MessagePayloadContentStr = &messageStr |
| 75 | + return nil |
| 76 | + } |
| 77 | + var messageArray []MessagePayloadContentBlock |
| 78 | + if err := json.Unmarshal(data, &messageArray); err == nil { |
| 79 | + m.MessagePayloadContentArray = messageArray |
| 80 | + return nil |
| 81 | + } |
| 82 | + return fmt.Errorf("failed to unmarshal MessagePayloadContent") |
| 83 | +} |
| 84 | + |
| 85 | +// ModelParameters contains the model configuration parameters |
| 86 | +type ModelParameters struct { |
| 87 | + N int `json:"n"` |
| 88 | + TopP float64 `json:"top_p"` |
| 89 | + Logprobs bool `json:"logprobs"` |
| 90 | + MaxTokens int `json:"max_tokens"` |
| 91 | + PromptTools []string `json:"promptTools"` |
| 92 | + Temperature float64 `json:"temperature"` |
| 93 | + PresencePenalty float64 `json:"presence_penalty"` |
| 94 | + FrequencyPenalty float64 `json:"frequency_penalty"` |
| 95 | +} |
| 96 | + |
| 97 | +// GetPromptVersion fetches a specific version of a prompt. |
| 98 | +// |
| 99 | +// Parameters: |
| 100 | +// - baseUrl: The base URL of the API endpoint. |
| 101 | +// - apiKey: The API key for authentication. |
| 102 | +// - versionId: The version ID you want to query. |
| 103 | +// - promptId: The prompt ID whose versions you want to query. |
| 104 | +// |
| 105 | +// Returns: |
| 106 | +// - *PromptVersion: The prompt version if successful, or nil with an error. |
| 107 | +// - *MaximError: An error if the request fails. |
| 108 | +func GetPromptVersion(baseUrl, apiKey, versionId, promptId string) (*PromptVersion, *MaximError) { |
| 109 | + url := fmt.Sprintf("%s/api/public/v1/prompts/versions?id=%s&promptId=%s", baseUrl, versionId, promptId) |
| 110 | + client := &http.Client{ |
| 111 | + Timeout: 15 * time.Second, |
| 112 | + } |
| 113 | + req, err := http.NewRequest("GET", url, nil) |
| 114 | + if err != nil { |
| 115 | + return nil, newMaximError(err) |
| 116 | + } |
| 117 | + req.Header.Set("Content-Type", "application/json") |
| 118 | + req.Header.Set("Accept", "application/json") |
| 119 | + req.Header.Set("x-maxim-api-key", apiKey) |
| 120 | + resp, err := client.Do(req) |
| 121 | + if err != nil { |
| 122 | + return nil, newMaximError(err) |
| 123 | + } |
| 124 | + defer resp.Body.Close() |
| 125 | + if resp.StatusCode != http.StatusOK { |
| 126 | + return nil, newMaximError(fmt.Errorf("unexpected status code: %d", resp.StatusCode)) |
| 127 | + } |
| 128 | + |
| 129 | + var response PromptVersionResponse |
| 130 | + err = json.NewDecoder(resp.Body).Decode(&response) |
| 131 | + if err != nil { |
| 132 | + return nil, newMaximError(err) |
| 133 | + } |
| 134 | + |
| 135 | + return &response.Data, nil |
| 136 | +} |
0 commit comments