エージェントみたいな感じで、プロンプトで正しい英語とか次の質問を出してくれるようにしてある。
中身
STT (Speech To Text)
WhisperModelを使って文字起こし。
from faster_whisper import WhisperModel
model = WhisperModel("base", compute_type="int8")
...
segments, _ = model.transcribe(path, language="en", task="transcribe")
return " ".join([s.text for s in segments]).strip()
LLM
こんな感じで現在の話題のサマリ、最後の質問を保持するようにしておき、プロンプトで返答とかを指定しておく。
summary = {
"topic": "",
"conversation_flow": "",
"user_level": "C"
}
last_question = "What topic should we talk about today?"
PROMPT = """
You are an English tutor.
The user is learning English.
Your tasks:
1. Correct the user's sentence
2. Explain mistakes in English
3. Ask the next question in simple English
4. Update the conversation summary
IMPORTANT RULES:
- Keep the conversation connected to the previous context
- The next question must naturally follow the conversation flow
- Ask 2 to 3 short sentences for the next question (not too long)
- Focus on grammar and word usage
- Keep explanations short and clear in English
- Use simple and natural English
- When someone suggests changing the topic, change the summary
STRICT OUTPUT RULES:
- Output ONLY valid JSON in English
- No extra text
- No markdown
{
"correction": "...",
"explanation": "...",
"next_question": "...",
"summary": {
"topic": "...",
"conversation_flow": "...",
"user_level": "..."
}
}
"""
prompt = f"""
{PROMPT}
Current summary:
{json.dumps(summary, ensure_ascii=False)}
Current question: {last_question}
User: {text}
"""
Ollamaに対しては以下のように投げる。
OLLAMA_URL = "http://localhost:11434/api/generate"
session = requests.Session()
response = session.post(
OLLAMA_URL,
json={
"model": "llama3.1",
"prompt": prompt,
"stream": False
},
timeout=60
)
TTS (Text to Speech)
piperでテキストを音声に変換
subprocess.run([
"piper",
"--model", "models/en_US-lessac-low.onnx",
"--output_file", output
], input=text, text=True)