You've already forked grok
send system prompt separated from user prompt
This commit is contained in:
47
main.go
47
main.go
@@ -24,25 +24,33 @@ type OpenAIPrompter struct {
|
|||||||
log *slog.Logger
|
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) {
|
func (p *OpenAIPrompter) Prompt(ctx context.Context, req PromptRequest) (*HighlyTrustedResponse, error) {
|
||||||
p.log.Info("new prompt request",
|
p.log.Info("new prompt request",
|
||||||
"req", req)
|
"req", req)
|
||||||
|
|
||||||
|
sysPrompt := composeSysPromptWithContext(p.cfg.SystemPrompt, req)
|
||||||
|
|
||||||
input := []responses.ResponseInputItemUnionParam{
|
input := []responses.ResponseInputItemUnionParam{
|
||||||
{
|
{
|
||||||
OfMessage: &responses.EasyInputMessageParam{
|
OfMessage: &responses.EasyInputMessageParam{
|
||||||
Content: responses.EasyInputMessageContentUnionParam{
|
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{
|
resp, err := p.cli.Responses.New(ctx, responses.ResponseNewParams{
|
||||||
Instructions: openai.String(p.cfg.SystemPrompt),
|
Instructions: openai.String(p.cfg.SystemPrompt),
|
||||||
Input: responses.ResponseNewParamsInputUnion{
|
Input: responses.ResponseNewParamsInputUnion{
|
||||||
@@ -105,7 +98,7 @@ type HighlyTrustedResponse struct {
|
|||||||
type PromptRequest struct {
|
type PromptRequest struct {
|
||||||
Username string
|
Username string
|
||||||
Question string
|
Question string
|
||||||
OriginalPostContent *string
|
ReplyToContent *string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Proompter interface {
|
type Proompter interface {
|
||||||
@@ -159,7 +152,7 @@ func (a *App) handleMessage(ctx context.Context, msg *tgbotapi.Message) error {
|
|||||||
|
|
||||||
response, err := a.proompter.Prompt(ctx, PromptRequest{
|
response, err := a.proompter.Prompt(ctx, PromptRequest{
|
||||||
Question: question,
|
Question: question,
|
||||||
OriginalPostContent: repliedToContent,
|
ReplyToContent: repliedToContent,
|
||||||
Username: username,
|
Username: username,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user