ref: Turn LLM into domain model to be implemented by Groq

This commit is contained in:
Marty Oehme 2025-06-09 22:10:40 +02:00
parent 80bb27fd47
commit 4ae093f367
Signed by: Marty
GPG key ID: 4E535BC19C61886E
2 changed files with 27 additions and 3 deletions

18
prophet/domain/llm.py Normal file
View file

@ -0,0 +1,18 @@
from typing import Protocol
from prophet.domain.original import Original
class LLMClient(Protocol):
def get_alternative_title_suggestions(self, original_content: str) -> str:
raise NotImplementedError
def rewrite_title(
self, original_content: str, suggestions: str | None = None
) -> str:
raise NotImplementedError
def rewrite_summary(
self, original: Original, improved_title: str | None = None
) -> str:
raise NotImplementedError

View file

@ -1,10 +1,11 @@
from groq import Groq from groq import Groq
from prophet.config import AiConfig from prophet.config import AiConfig
from prophet.domain.llm import LLMClient
from prophet.domain.original import Original from prophet.domain.original import Original
class GroqClient: class GroqClient(LLMClient):
config_ai: AiConfig config_ai: AiConfig
client: Groq client: Groq
@ -57,7 +58,12 @@ class GroqClient:
raise ValueError raise ValueError
return winner_str.strip(" \"'") return winner_str.strip(" \"'")
def rewrite_summary(self, orig: Original, improved_title: str) -> str: def rewrite_summary(
self, original: Original, improved_title: str | None = None
) -> str:
if not improved_title:
improved_title = self.rewrite_title(original.title)
no_shocking_turn: bool = True no_shocking_turn: bool = True
summary = self.client.chat.completions.create( summary = self.client.chat.completions.create(
messages=[ messages=[
@ -66,7 +72,7 @@ class GroqClient:
"content": f""" "content": f"""
Below there is an original title and an original summary. Then follows an improved title. Write an improved summary based on the original summary which fits to the improved title. Below there is an original title and an original summary. Then follows an improved title. Write an improved summary based on the original summary which fits to the improved title.
{"Do not use the phrase: 'in a surprising turn of events' or 'in a shocking turn of events.'" if no_shocking_turn else ""} {"Do not use the phrase: 'in a surprising turn of events' or 'in a shocking turn of events.'" if no_shocking_turn else ""}
Only output the improved summary.\n\nTitle:{orig.title}\nSummary:{orig.summary}\n---\nTitle:{improved_title}\nSummary:""", Only output the improved summary.\n\nTitle:{original.title}\nSummary:{original.summary}\n---\nTitle:{improved_title}\nSummary:""",
} }
], ],
model="llama-3.3-70b-versatile", model="llama-3.3-70b-versatile",