Add basic joke improvements with Groq

This commit is contained in:
Marty Oehme 2025-06-05 15:12:45 +02:00
parent 68ef1de2e4
commit fd7d73e9c3
Signed by: Marty
GPG key ID: 4E535BC19C61886E
3 changed files with 69 additions and 2 deletions

View file

@ -1,5 +1,35 @@
import os
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from groq import Groq
def improve_with_groq(original: str) -> str:
client = Groq(api_key=os.getenv("GROQ_API_KEY", "NO_API_KEY_FOUND"))
suggestions = client.chat.completions.create(
messages=[
{
"role": "user",
"content": f"Improve on the following satirical headline. The headline should be funny, can involve current political events and should have an edge to it. Print only the suggestions, with one suggestion on each line.\nOriginal: '{original}'",
}
],
model="llama-3.3-70b-versatile",
)
print("Suggestions: ", suggestions.choices[0].message.content)
winner = client.chat.completions.create(
messages=[
{
"role": "user",
"content": f"From the following satirical headline suggestions, pick the strongest one and print it. Do not print anything else. The chosen suggestion should be the most edgy, able to trend on social media and funniest. The suggestions:\n\n{suggestions}",
}
],
model="llama-3.3-70b-versatile",
)
print("Winner: ", winner.choices[0].message.content)
winner_str = winner.choices[0].message.content
if not winner_str:
raise ValueError
return winner_str
app = FastAPI()
@ -18,6 +48,10 @@ app.add_middleware(
@app.get("/test")
@app.get("/improve")
def improve_headline(content: str):
return improve_with_groq(content)
def read_root():
response = {
"data": [

View file

@ -4,7 +4,10 @@ version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.13"
dependencies = ["fastapi[standard]>=0.115.12"]
dependencies = [
"fastapi[standard]>=0.115.12",
"groq>=0.26.0",
]
[dependency-groups]
dev = ["mypy>=1.16.0", "ruff>=0.11.12"]

32
uv.lock generated
View file

@ -54,6 +54,15 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/d1/d6/3965ed04c63042e047cb6a3e6ed1a63a35087b6a609aa3a15ed8ac56c221/colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6", size = 25335, upload-time = "2022-10-25T02:36:20.889Z" },
]
[[package]]
name = "distro"
version = "1.9.0"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/fc/f8/98eea607f65de6527f8a2e8885fc8015d3e6f5775df186e443e0964a11c3/distro-1.9.0.tar.gz", hash = "sha256:2fa77c6fd8940f116ee1d6b94a2f90b13b5ea8d019b98bc8bafdcabcdd9bdbed", size = 60722, upload-time = "2023-12-24T09:54:32.31Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/12/b3/231ffd4ab1fc9d679809f356cebee130ac7daa00d6d6f3206dd4fd137e9e/distro-1.9.0-py3-none-any.whl", hash = "sha256:7bffd925d65168f85027d8da9af6bddab658135b840670a223589bc0c8ef02b2", size = 20277, upload-time = "2023-12-24T09:54:30.421Z" },
]
[[package]]
name = "dnspython"
version = "2.7.0"
@ -119,6 +128,23 @@ standard = [
{ name = "uvicorn", extra = ["standard"] },
]
[[package]]
name = "groq"
version = "0.26.0"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "anyio" },
{ name = "distro" },
{ name = "httpx" },
{ name = "pydantic" },
{ name = "sniffio" },
{ name = "typing-extensions" },
]
sdist = { url = "https://files.pythonhosted.org/packages/b6/68/a820a22937e4a2f48b7a60e1aaf7948fb57d1c124072829b5cc06a01cfa0/groq-0.26.0.tar.gz", hash = "sha256:1f1e50d26c6134f6fb580ea7002e8f9ff5c7c1685c9e0f50d71adecd039ae5d4", size = 128500, upload-time = "2025-05-29T18:25:23.332Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/da/2e/14bef74aa760199c7a179c753953512d1aa1ed264f7477f6bd6fe9b9cff3/groq-0.26.0-py3-none-any.whl", hash = "sha256:4dc0900d506876ea39a9aa1985f12a51859bacf486fe939664248eff1f451af3", size = 129572, upload-time = "2025-05-29T18:25:22.077Z" },
]
[[package]]
name = "h11"
version = "0.16.0"
@ -285,6 +311,7 @@ version = "0.1.0"
source = { editable = "." }
dependencies = [
{ name = "fastapi", extra = ["standard"] },
{ name = "groq" },
]
[package.dev-dependencies]
@ -294,7 +321,10 @@ dev = [
]
[package.metadata]
requires-dist = [{ name = "fastapi", extras = ["standard"], specifier = ">=0.115.12" }]
requires-dist = [
{ name = "fastapi", extras = ["standard"], specifier = ">=0.115.12" },
{ name = "groq", specifier = ">=0.26.0" },
]
[package.metadata.requires-dev]
dev = [