mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-04 23:16:57 +08:00
refactor: adds a message to the conversation history
This commit is contained in:
@@ -1 +1 @@
|
|||||||
v5.0.0-beta-2504281959
|
v5.0.0-beta-2504282012
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ type UITarsAsserter struct {
|
|||||||
model *ark.ChatModel
|
model *ark.ChatModel
|
||||||
config *ark.ChatModelConfig
|
config *ark.ChatModelConfig
|
||||||
systemPrompt string
|
systemPrompt string
|
||||||
history []*schema.Message // conversation history
|
history ConversationHistory
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewUITarsAsserter creates a new UITarsAsserter instance
|
// NewUITarsAsserter creates a new UITarsAsserter instance
|
||||||
@@ -87,7 +87,7 @@ func (a *UITarsAsserter) Assert(opts *AssertOptions) (*AssertionResponse, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Reset history for each new assertion
|
// Reset history for each new assertion
|
||||||
a.history = []*schema.Message{
|
a.history = ConversationHistory{
|
||||||
{
|
{
|
||||||
Role: schema.System,
|
Role: schema.System,
|
||||||
Content: a.systemPrompt,
|
Content: a.systemPrompt,
|
||||||
@@ -118,7 +118,7 @@ Here is the assertion. Please tell whether it is truthy according to the screens
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Append user message to history
|
// Append user message to history
|
||||||
appendConversationHistory(&a.history, userMsg)
|
a.history.Append(userMsg)
|
||||||
|
|
||||||
// Call model service, generate response
|
// Call model service, generate response
|
||||||
logRequest(a.history)
|
logRequest(a.history)
|
||||||
@@ -138,7 +138,7 @@ Here is the assertion. Please tell whether it is truthy according to the screens
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Append assistant message to history
|
// Append assistant message to history
|
||||||
appendConversationHistory(&a.history, &schema.Message{
|
a.history.Append(&schema.Message{
|
||||||
Role: schema.Assistant,
|
Role: schema.Assistant,
|
||||||
Content: resp.Content,
|
Content: resp.Content,
|
||||||
})
|
})
|
||||||
|
|||||||
+13
-11
@@ -131,8 +131,10 @@ func logResponse(resp *schema.Message) {
|
|||||||
logger.Msg("log response message")
|
logger.Msg("log response message")
|
||||||
}
|
}
|
||||||
|
|
||||||
// appendConversationHistory adds a message to the conversation history
|
type ConversationHistory []*schema.Message
|
||||||
func appendConversationHistory(history *[]*schema.Message, msg *schema.Message) {
|
|
||||||
|
// Append adds a message to the conversation history
|
||||||
|
func (h *ConversationHistory) Append(msg *schema.Message) {
|
||||||
// for user image message:
|
// for user image message:
|
||||||
// - keep at most 4 user image messages
|
// - keep at most 4 user image messages
|
||||||
// - delete the oldest user image message when the limit is reached
|
// - delete the oldest user image message when the limit is reached
|
||||||
@@ -142,7 +144,7 @@ func appendConversationHistory(history *[]*schema.Message, msg *schema.Message)
|
|||||||
firstUserImgIndex := -1
|
firstUserImgIndex := -1
|
||||||
|
|
||||||
// calculate the number of user messages and find the index of the first user message
|
// calculate the number of user messages and find the index of the first user message
|
||||||
for i, item := range *history {
|
for i, item := range *h {
|
||||||
if item.Role == schema.User {
|
if item.Role == schema.User {
|
||||||
userImgCount++
|
userImgCount++
|
||||||
if firstUserImgIndex == -1 {
|
if firstUserImgIndex == -1 {
|
||||||
@@ -154,28 +156,28 @@ func appendConversationHistory(history *[]*schema.Message, msg *schema.Message)
|
|||||||
// if there are already 4 user messages, delete the first one before adding the new message
|
// if there are already 4 user messages, delete the first one before adding the new message
|
||||||
if userImgCount >= 4 && firstUserImgIndex >= 0 {
|
if userImgCount >= 4 && firstUserImgIndex >= 0 {
|
||||||
// delete the first user message
|
// delete the first user message
|
||||||
*history = append(
|
*h = append(
|
||||||
(*history)[:firstUserImgIndex],
|
(*h)[:firstUserImgIndex],
|
||||||
(*history)[firstUserImgIndex+1:]...,
|
(*h)[firstUserImgIndex+1:]...,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
// add the new user message to the history
|
// add the new user message to the history
|
||||||
*history = append(*history, msg)
|
*h = append(*h, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// for assistant message:
|
// for assistant message:
|
||||||
// - keep at most the last 10 assistant messages
|
// - keep at most the last 10 assistant messages
|
||||||
if msg.Role == schema.Assistant {
|
if msg.Role == schema.Assistant {
|
||||||
// add the new assistant message to the history
|
// add the new assistant message to the history
|
||||||
*history = append(*history, msg)
|
*h = append(*h, msg)
|
||||||
|
|
||||||
// if there are more than 10 assistant messages, remove the oldest ones
|
// if there are more than 10 assistant messages, remove the oldest ones
|
||||||
assistantMsgCount := 0
|
assistantMsgCount := 0
|
||||||
for i := len(*history) - 1; i >= 0; i-- {
|
for i := len(*h) - 1; i >= 0; i-- {
|
||||||
if (*history)[i].Role == schema.Assistant {
|
if (*h)[i].Role == schema.Assistant {
|
||||||
assistantMsgCount++
|
assistantMsgCount++
|
||||||
if assistantMsgCount > 10 {
|
if assistantMsgCount > 10 {
|
||||||
*history = append((*history)[:i], (*history)[i+1:]...)
|
*h = append((*h)[:i], (*h)[i+1:]...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ type Planner struct {
|
|||||||
model model.ToolCallingChatModel
|
model model.ToolCallingChatModel
|
||||||
config *openai.ChatModelConfig
|
config *openai.ChatModelConfig
|
||||||
systemPrompt string
|
systemPrompt string
|
||||||
history []*schema.Message // conversation history
|
history ConversationHistory
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call performs UI planning using Vision Language Model
|
// Call performs UI planning using Vision Language Model
|
||||||
@@ -124,7 +124,7 @@ func (p *Planner) Call(opts *PlanningOptions) (*PlanningResult, error) {
|
|||||||
if len(p.history) == 0 {
|
if len(p.history) == 0 {
|
||||||
// add system message
|
// add system message
|
||||||
systemPrompt := uiTarsPlanningPrompt + opts.UserInstruction
|
systemPrompt := uiTarsPlanningPrompt + opts.UserInstruction
|
||||||
p.history = []*schema.Message{
|
p.history = ConversationHistory{
|
||||||
{
|
{
|
||||||
Role: schema.System,
|
Role: schema.System,
|
||||||
Content: systemPrompt,
|
Content: systemPrompt,
|
||||||
@@ -132,7 +132,7 @@ func (p *Planner) Call(opts *PlanningOptions) (*PlanningResult, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// append user image message
|
// append user image message
|
||||||
appendConversationHistory(&p.history, opts.Message)
|
p.history.Append(opts.Message)
|
||||||
|
|
||||||
// call model service, generate response
|
// call model service, generate response
|
||||||
logRequest(p.history)
|
logRequest(p.history)
|
||||||
@@ -152,7 +152,7 @@ func (p *Planner) Call(opts *PlanningOptions) (*PlanningResult, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// append assistant message
|
// append assistant message
|
||||||
appendConversationHistory(&p.history, &schema.Message{
|
p.history.Append(&schema.Message{
|
||||||
Role: schema.Assistant,
|
Role: schema.Assistant,
|
||||||
Content: result.ActionSummary,
|
Content: result.ActionSummary,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -123,7 +123,7 @@ type UITarsPlanner struct {
|
|||||||
model model.ToolCallingChatModel
|
model model.ToolCallingChatModel
|
||||||
config *ark.ChatModelConfig
|
config *ark.ChatModelConfig
|
||||||
systemPrompt string
|
systemPrompt string
|
||||||
history []*schema.Message // conversation history
|
history ConversationHistory
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call performs UI planning using Vision Language Model
|
// Call performs UI planning using Vision Language Model
|
||||||
@@ -137,7 +137,7 @@ func (p *UITarsPlanner) Call(opts *PlanningOptions) (*PlanningResult, error) {
|
|||||||
if len(p.history) == 0 {
|
if len(p.history) == 0 {
|
||||||
// add system message
|
// add system message
|
||||||
systemPrompt := uiTarsPlanningPrompt + opts.UserInstruction
|
systemPrompt := uiTarsPlanningPrompt + opts.UserInstruction
|
||||||
p.history = []*schema.Message{
|
p.history = ConversationHistory{
|
||||||
{
|
{
|
||||||
Role: schema.System,
|
Role: schema.System,
|
||||||
Content: systemPrompt,
|
Content: systemPrompt,
|
||||||
@@ -145,7 +145,7 @@ func (p *UITarsPlanner) Call(opts *PlanningOptions) (*PlanningResult, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// append user image message
|
// append user image message
|
||||||
appendConversationHistory(&p.history, opts.Message)
|
p.history.Append(opts.Message)
|
||||||
|
|
||||||
// call model service, generate response
|
// call model service, generate response
|
||||||
logRequest(p.history)
|
logRequest(p.history)
|
||||||
@@ -165,7 +165,7 @@ func (p *UITarsPlanner) Call(opts *PlanningOptions) (*PlanningResult, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// append assistant message
|
// append assistant message
|
||||||
appendConversationHistory(&p.history, &schema.Message{
|
p.history.Append(&schema.Message{
|
||||||
Role: schema.Assistant,
|
Role: schema.Assistant,
|
||||||
Content: result.ActionSummary,
|
Content: result.ActionSummary,
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user