|
| 1 | +// Copyright (c) 2021 Terminus, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package context |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "encoding/json" |
| 20 | + "net/http" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "sigs.k8s.io/yaml" |
| 24 | + |
| 25 | + "github.com/erda-project/erda/internal/apps/ai-proxy/common" |
| 26 | + "github.com/erda-project/erda/internal/apps/ai-proxy/common/ctxhelper" |
| 27 | + "github.com/erda-project/erda/pkg/reverseproxy" |
| 28 | +) |
| 29 | + |
| 30 | +// ContentType represents the type of content in the message |
| 31 | +type ContentType string |
| 32 | + |
| 33 | +const ( |
| 34 | + Name = "context-responses" |
| 35 | + ContentTypeInputText ContentType = "input_text" |
| 36 | +) |
| 37 | + |
| 38 | +var ( |
| 39 | + _ reverseproxy.RequestFilter = (*Context)(nil) |
| 40 | +) |
| 41 | + |
| 42 | +func init() { |
| 43 | + reverseproxy.RegisterFilterCreator(Name, New) |
| 44 | +} |
| 45 | + |
| 46 | +type ( |
| 47 | + // Message represents a message in the conversation |
| 48 | + Message struct { |
| 49 | + Role string `json:"role"` |
| 50 | + Content any `json:"content"` |
| 51 | + } |
| 52 | + // ContentObject represents a content object in the message |
| 53 | + ContentObject struct { |
| 54 | + Type string `json:"type"` |
| 55 | + Text string `json:"text"` |
| 56 | + } |
| 57 | +) |
| 58 | + |
| 59 | +type Context struct { |
| 60 | + Config *Config |
| 61 | +} |
| 62 | + |
| 63 | +type Config struct { |
| 64 | +} |
| 65 | + |
| 66 | +func New(configJSON json.RawMessage) (reverseproxy.Filter, error) { |
| 67 | + var cfg Config |
| 68 | + if err := yaml.Unmarshal(configJSON, &cfg); err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + return &Context{Config: &cfg}, nil |
| 72 | +} |
| 73 | + |
| 74 | +func (f *Context) OnRequest(ctx context.Context, w http.ResponseWriter, infor reverseproxy.HttpInfor) (signal reverseproxy.Signal, err error) { |
| 75 | + if common.GetRequestRoutePath(ctx) == common.RequestPathPrefixV1Responses && infor.Method() == http.MethodPost { |
| 76 | + var req map[string]any |
| 77 | + if err := json.NewDecoder(infor.BodyBuffer()).Decode(&req); err != nil { |
| 78 | + return reverseproxy.Intercept, err |
| 79 | + } |
| 80 | + |
| 81 | + prompts := make([]string, 0) |
| 82 | + |
| 83 | + if instructions, ok := req["instructions"].(string); ok && strings.TrimSpace(instructions) != "" { |
| 84 | + prompts = append(prompts, instructions) |
| 85 | + } |
| 86 | + |
| 87 | + // Handle OpenAI Responses API input structure |
| 88 | + if input, ok := req["input"]; ok { |
| 89 | + prompts = append(prompts, FindUserPrompts(input)...) |
| 90 | + } |
| 91 | + |
| 92 | + ctxhelper.PutUserPrompt(ctx, strings.Join(prompts, "\n")) |
| 93 | + infor.SetBody2(req) |
| 94 | + } |
| 95 | + |
| 96 | + return reverseproxy.Continue, nil |
| 97 | +} |
| 98 | + |
| 99 | +// FindUserPrompts recursively extracts user prompts from OpenAI Responses API structure |
| 100 | +func FindUserPrompts(obj any) []string { |
| 101 | + if obj == nil { |
| 102 | + return nil |
| 103 | + } |
| 104 | + |
| 105 | + prompts := make([]string, 0) |
| 106 | + switch v := obj.(type) { |
| 107 | + case string: |
| 108 | + if strings.TrimSpace(v) != "" { |
| 109 | + prompts = append(prompts, v) |
| 110 | + } |
| 111 | + case []any: |
| 112 | + for _, item := range v { |
| 113 | + if msg, ok := item.(map[string]any); ok { |
| 114 | + if role, ok := msg["role"].(string); ok && role == "user" { |
| 115 | + if content, ok := msg["content"]; ok { |
| 116 | + prompts = append(prompts, extractPromptsFromContent(content)...) |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return prompts |
| 124 | +} |
| 125 | + |
| 126 | +// extractPromptsFromContent extracts prompts from content of different types |
| 127 | +func extractPromptsFromContent(content any) []string { |
| 128 | + prompts := make([]string, 0) |
| 129 | + |
| 130 | + switch c := content.(type) { |
| 131 | + case string: |
| 132 | + if strings.TrimSpace(c) != "" { |
| 133 | + prompts = append(prompts, c) |
| 134 | + } |
| 135 | + case []any: |
| 136 | + for _, cc := range c { |
| 137 | + if contentObj, ok := cc.(map[string]any); ok { |
| 138 | + if contentType, ok := contentObj["type"].(string); ok && ContentType(contentType) == ContentTypeInputText { |
| 139 | + if text, ok := contentObj["text"].(string); ok && text != "" { |
| 140 | + prompts = append(prompts, text) |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return prompts |
| 148 | +} |
0 commit comments