Updated open API Syntax

This commit is contained in:
2025-04-30 18:24:44 +10:00
parent c292b94da7
commit d7fd57af81
7 changed files with 51 additions and 35 deletions
+10 -6
View File
@@ -127,7 +127,6 @@ def generate_article_tweet(author, post, persona):
url = post["url"]
author_handle = f"@{author['username']}"
# Base tweet content
prompt = (
f"Generate a concise tweet (under 280 characters) for {author_handle} using the persona '{persona}'. "
f"Summarize the article '{title}' and include the link '{url}'. "
@@ -137,8 +136,8 @@ def generate_article_tweet(author, post, persona):
f"Do not include hashtags or emojis."
)
response = openai.ChatCompletion.create(
model=SUMMARY_MODEL,
response = openai.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a social media expert crafting engaging tweets."},
{"role": "user", "content": prompt}
@@ -307,7 +306,7 @@ def smart_image_and_filter(title, summary):
"for an image search about food industry trends or viral content. Prioritize specific terms if present, "
"otherwise focus on the main theme. "
"Return 'SKIP' if the article is about home appliances, recipes, promotions, or contains 'homemade', else 'KEEP'. "
"Return as JSON: {'image_query': 'specific term', 'relevance': ['keyword1', 'keyword2'], 'action': 'KEEP' or 'SKIP'}"
"Return as JSON with double quotes for all property names and string values (e.g., {\"image_query\": \"specific term\", \"relevance\": [\"keyword1\", \"keyword2\"], \"action\": \"KEEP\" or \"SKIP\"})."
)
response = client.chat.completions.create(
@@ -321,11 +320,15 @@ def smart_image_and_filter(title, summary):
raw_result = response.choices[0].message.content.strip()
logging.info(f"Raw GPT smart image/filter response: '{raw_result}'")
# Remove ```json markers and fix single quotes in JSON structure
cleaned_result = re.sub(r'```json\s*|\s*```', '', raw_result).strip()
# Replace single quotes with double quotes, but preserve single quotes within string values
fixed_result = re.sub(r"(?<!\\)'(?=\s*[\w\s]*\])|(?<=\[|\{|\s)'|'(?=\s*[\]\},:])|(?<=\w)'(?=\s*:)", '"', cleaned_result)
try:
result = json.loads(cleaned_result)
result = json.loads(fixed_result)
except json.JSONDecodeError as e:
logging.warning(f"JSON parsing failed: {e}, raw: '{cleaned_result}'. Using fallback.")
logging.warning(f"JSON parsing failed: {e}, raw: '{fixed_result}'. Using fallback.")
return "food trends", ["cuisine", "dining"], False
if not isinstance(result, dict) or "image_query" not in result or "relevance" not in result or "action" not in result:
@@ -468,6 +471,7 @@ def summarize_with_gpt4o(content, source_name, link, interest_score=0, extra_pro
full_prompt = (
f"{prompt}\n\n"
f"{extra_prompt}\n\n"
f"Avoid using the word 'elevate'—use more humanized language like 'level up' or 'bring to life'.\n"
f"Content to summarize:\n{content}\n\n"
f"Source: {source_name}\n"
f"Link: {link}"