Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Chat History Defaults to Spanish Despite English Language Selected #294

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion backend/app/routes/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ def patch_conversation_title(


@router.get(
"/conversation/{conversation_id}/proposed-title", response_model=ProposedTitle
"/conversation/{conversation_id}/proposed-title/{language}",
response_model=ProposedTitle,
)
def get_proposed_title(request: Request, conversation_id: str):
"""Suggest conversation title"""
Expand Down
5 changes: 3 additions & 2 deletions backend/app/usecases/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ def chat(user_id: str, chat_input: ChatInput) -> ChatOutput:
def propose_conversation_title(
user_id: str,
conversation_id: str,
language: str = "en",
model: Literal[
"claude-instant-v1",
"claude-v2",
Expand All @@ -423,12 +424,12 @@ def propose_conversation_title(
"mistral-large",
] = "claude-v3-haiku",
) -> str:
PROMPT = """Reading the conversation above, what is the appropriate title for the conversation? When answering the title, please follow the rules below:
PROMPT = f"""Reading the conversation above, what is the appropriate title for the conversation? When answering the title, please follow the rules below:
<rules>
- Title length must be from 15 to 20 characters.
- Prefer more specific title than general. Your title should always be distinct from others.
- Return the conversation title only. DO NOT include any strings other than the title.
- Title must be in the same language as the conversation.
- Title must be in {language}.
</rules>
"""
# Fetch existing conversation
Expand Down
47 changes: 47 additions & 0 deletions backend/tests/test_usecases/test_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
create_test_private_bot,
create_test_public_bot,
)
import pytest
from langdetect import detect
from pydantic import ValidationError


MODEL: type_model_name = "claude-instant-v1"
MISTRAL_MODEL: type_model_name = "mistral-7b-instruct"
Expand Down Expand Up @@ -526,6 +530,18 @@ def setUp(self) -> None:
self.mistral_output = mistral_output
print(mistral_output)

self.example_texts = {
"en": "What are the top tourist attractions in New York?",
"ja": "日本の有名な観光地はどこですか?",
"ko": "서울의 주요 관광지는 어디인가요?",
"es": "¿Cuáles son los principales destinos turísticos en España?",
"zh-Hans": "中国的主要旅游景点在哪里?",
"zh-Hant": "台灣的主要旅遊景點在哪裡?",
"fr": "Quelles sont les principales attractions touristiques en France?",
"de": "Was sind die Haupttouristenattraktionen in Deutschland?",
"it": "Quali sono le principali attrazioni turistiche in Italia?",
}

def test_propose_title(self):
title = propose_conversation_title("user1", self.output.conversation_id)
print(f"[title]: {title}")
Expand All @@ -534,6 +550,37 @@ def test_propose_title_mistral(self):
title = propose_conversation_title("user1", self.mistral_output.conversation_id)
print(f"[title]: {title}")

def test_language_titles(self):
for lang_code, text in self.example_texts.items():
with self.subTest(lang_code=lang_code):
# Mock your chat function or use the actual one if possible
chat_input = ChatInput(
conversation_id="test_conversation_id",
message=MessageInput(
role="user",
content=[Content(content_type="text", body=text)],
model="MODEL_NAME", # Replace with actual model variable
parent_message_id=None,
message_id=None,
),
bot_id=None,
)
output = chat(self.user_id, chat_input)
title, detected_language = propose_conversation_title(
self.user_id, output.conversation_id, lang_code
)
self.assertEqual(
detected_language,
lang_code,
f"Expected language {lang_code}, but got {detected_language}",
)
detected_title_lang = detect(title)
self.assertEqual(
detected_title_lang,
lang_code,
f"Title language mismatch: expected {lang_code}, detected {detected_title_lang}",
)

def tearDown(self) -> None:
delete_conversation_by_id("user1", self.output.conversation_id)

Expand Down
14 changes: 12 additions & 2 deletions frontend/src/hooks/useConversationApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ import {
PostMessageResponse,
} from '../@types/conversation';
import useHttp from './useHttp';
import { useTranslation } from 'react-i18next';
import '../i18n';


const useConversationApi = () => {
const http = useHttp();
const { mutate } = useSWRConfig();


const { i18n } = useTranslation();
const currentLanguage = i18n.language

const updateTitle = (conversationId: string, title: string) => {
return http.patch(`conversation/${conversationId}/title`, {
newTitle: title,
Expand Down Expand Up @@ -53,10 +60,13 @@ const useConversationApi = () => {
return http.delete('conversations');
},
updateTitle,
updateTitleWithGeneratedTitle: async (conversationId: string) => {
updateTitleWithGeneratedTitle: async (conversationId: string, language: string = "en") => {

language = currentLanguage.toLowerCase();

const res = await http.getOnce<{
title: string;
}>(`conversation/${conversationId}/proposed-title`);
}>(`conversation/${conversationId}/proposed-title/${language}`);
return updateTitle(conversationId, res.data.title);
},
mutateConversations: (
Expand Down
Loading