mirror of
https://github.com/AstrBotDevs/AstrBot.git
synced 2026-08-30 17:33:24 +08:00
fix(provider): preserve current user turn in Gemini history (#9738)
* fix: preserve current user turn in Gemini history * test(provider): cover Gemini assistant-ending history
This commit is contained in:
@@ -428,7 +428,7 @@ class ProviderGoogleGenAI(Provider):
|
||||
append_or_extend(gemini_contents, parts, types.UserContent)
|
||||
|
||||
if gemini_contents and isinstance(gemini_contents[0], types.ModelContent):
|
||||
gemini_contents.pop()
|
||||
gemini_contents.pop(0)
|
||||
|
||||
return gemini_contents
|
||||
|
||||
|
||||
@@ -2,13 +2,74 @@ from types import SimpleNamespace
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from google.genai import types
|
||||
|
||||
from astrbot.core.exceptions import EmptyModelOutputError
|
||||
import astrbot.core.provider.sources.request_retry as request_retry
|
||||
from astrbot.core.exceptions import EmptyModelOutputError
|
||||
from astrbot.core.provider.entities import LLMResponse
|
||||
from astrbot.core.provider.sources.gemini_source import ProviderGoogleGenAI
|
||||
|
||||
|
||||
def test_gemini_prepare_conversation_removes_leading_model_content():
|
||||
provider = ProviderGoogleGenAI.__new__(ProviderGoogleGenAI)
|
||||
|
||||
contents = provider._prepare_conversation(
|
||||
{
|
||||
"messages": [
|
||||
{"role": "assistant", "content": "stale assistant turn"},
|
||||
{"role": "user", "content": "current user turn"},
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
assert len(contents) == 1
|
||||
assert isinstance(contents[0], types.UserContent)
|
||||
assert contents[0].parts is not None
|
||||
assert contents[0].parts[-1].text == "current user turn"
|
||||
|
||||
|
||||
def test_gemini_prepare_conversation_keeps_normal_user_first_history():
|
||||
provider = ProviderGoogleGenAI.__new__(ProviderGoogleGenAI)
|
||||
|
||||
contents = provider._prepare_conversation(
|
||||
{
|
||||
"messages": [
|
||||
{"role": "user", "content": "first user turn"},
|
||||
{"role": "assistant", "content": "assistant turn"},
|
||||
{"role": "user", "content": "current user turn"},
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
assert [type(content) for content in contents] == [
|
||||
types.UserContent,
|
||||
types.ModelContent,
|
||||
types.UserContent,
|
||||
]
|
||||
assert contents[-1].parts is not None
|
||||
assert contents[-1].parts[-1].text == "current user turn"
|
||||
|
||||
|
||||
def test_gemini_prepare_conversation_preserves_user_model_history():
|
||||
provider = ProviderGoogleGenAI.__new__(ProviderGoogleGenAI)
|
||||
|
||||
contents = provider._prepare_conversation(
|
||||
{
|
||||
"messages": [
|
||||
{"role": "user", "content": "user turn"},
|
||||
{"role": "assistant", "content": "assistant turn"},
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
assert [type(content) for content in contents] == [
|
||||
types.UserContent,
|
||||
types.ModelContent,
|
||||
]
|
||||
assert contents[-1].parts is not None
|
||||
assert contents[-1].parts[-1].text == "assistant turn"
|
||||
|
||||
|
||||
def test_gemini_empty_output_raises_empty_model_output_error():
|
||||
llm_response = LLMResponse(role="assistant")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user