You've already forked grok
send system prompt separated from user prompt
This commit is contained in:
55
main.go
55
main.go
@@ -24,25 +24,33 @@ type OpenAIPrompter struct {
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
func composeSysPromptWithContext(systemPrompt string, req PromptRequest) string {
|
||||
var b strings.Builder
|
||||
|
||||
b.WriteString(systemPrompt)
|
||||
b.WriteString("\nИмя пользователя: ")
|
||||
b.WriteString(req.Username)
|
||||
|
||||
if req.ReplyToContent != nil {
|
||||
b.WriteString("\nПользователь отсылается на текст сообщения: ")
|
||||
b.WriteString(*req.ReplyToContent)
|
||||
}
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (p *OpenAIPrompter) Prompt(ctx context.Context, req PromptRequest) (*HighlyTrustedResponse, error) {
|
||||
p.log.Info("new prompt request",
|
||||
"req", req)
|
||||
|
||||
sysPrompt := composeSysPromptWithContext(p.cfg.SystemPrompt, req)
|
||||
|
||||
input := []responses.ResponseInputItemUnionParam{
|
||||
{
|
||||
OfMessage: &responses.EasyInputMessageParam{
|
||||
Content: responses.EasyInputMessageContentUnionParam{
|
||||
OfString: openai.String(p.cfg.SystemPrompt),
|
||||
OfString: openai.String(sysPrompt),
|
||||
},
|
||||
Role: responses.EasyInputMessageRoleSystem,
|
||||
},
|
||||
},
|
||||
{
|
||||
OfMessage: &responses.EasyInputMessageParam{
|
||||
Content: responses.EasyInputMessageContentUnionParam{
|
||||
OfString: openai.String("Пользователя зовут " + req.Username),
|
||||
},
|
||||
Role: responses.EasyInputMessageRoleSystem,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -55,21 +63,6 @@ func (p *OpenAIPrompter) Prompt(ctx context.Context, req PromptRequest) (*Highly
|
||||
},
|
||||
}
|
||||
|
||||
if req.OriginalPostContent != nil {
|
||||
message := "Пользователь может ссылаться на текст поста, который он комментирует. Вот он: " +
|
||||
*req.OriginalPostContent
|
||||
|
||||
input = append(input, responses.ResponseInputItemUnionParam{
|
||||
OfMessage: &responses.EasyInputMessageParam{
|
||||
Content: responses.EasyInputMessageContentUnionParam{
|
||||
OfString: openai.String(message),
|
||||
},
|
||||
Role: responses.EasyInputMessageRoleDeveloper,
|
||||
Type: responses.EasyInputMessageTypeMessage,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
resp, err := p.cli.Responses.New(ctx, responses.ResponseNewParams{
|
||||
Instructions: openai.String(p.cfg.SystemPrompt),
|
||||
Input: responses.ResponseNewParamsInputUnion{
|
||||
@@ -103,9 +96,9 @@ type HighlyTrustedResponse struct {
|
||||
}
|
||||
|
||||
type PromptRequest struct {
|
||||
Username string
|
||||
Question string
|
||||
OriginalPostContent *string
|
||||
Username string
|
||||
Question string
|
||||
ReplyToContent *string
|
||||
}
|
||||
|
||||
type Proompter interface {
|
||||
@@ -158,9 +151,9 @@ func (a *App) handleMessage(ctx context.Context, msg *tgbotapi.Message) error {
|
||||
}
|
||||
|
||||
response, err := a.proompter.Prompt(ctx, PromptRequest{
|
||||
Question: question,
|
||||
OriginalPostContent: repliedToContent,
|
||||
Username: username,
|
||||
Question: question,
|
||||
ReplyToContent: repliedToContent,
|
||||
Username: username,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("prompting: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user