Put the three providers in a table, and test the request rather than the list

Reworked on top of master. The Groq request itself needed nothing beyond a key,
a base URL and a model id, but two providers fit in an if and an else where
three do not, and each one was written out four times: in transcribe_target(),
in the key rows of the settings window, in save and in load.

config.TRANSCRIBERS holds them now, one row each: the name the user sees and
the three settings that keep its key, its endpoint and its model. The variable
an empty key falls back to is the name of its setting, shouted. The provider
box, save, load, `dikte models --provider` and `dikte test-key` all read that
table, so a fourth provider is a row and a default rather than a branch in five
files. The key field, its Test button and its answer line are built once and
the three signals became one that carries which key was asked about.

The tests moved into the files of the modules they cover and check what a
provider actually changes: that the request goes to api.groq.com with the right
model and fields, that the glossary now reaches everything except OpenRouter,
that a Groq error says Groq, and that the settings window carries the key and
the model there and back. GROQ_API_KEY is cleared for the test run like the
other two, so a developer who has one does not send a test to the network.

Co-authored-by: Muzaffer Emre <[email protected]>
This commit is contained in:
yusufipk
2026-08-01 22:09:41 +07:00
co-authored by Muzaffer Emre
parent 22edd2f247
commit d04efe235a
13 changed files with 298 additions and 212 deletions
+33 -17
View File
@@ -1,5 +1,6 @@
"""Settings storage in ~/.config/dikte/config.json"""
import collections
import hashlib
import json
import os
@@ -367,7 +368,7 @@ DEFAULTS = {
"groq_base_url": "https://api.groq.com/openai/v1",
"openrouter_api_key": "",
"openrouter_base_url": "https://openrouter.ai/api/v1",
"transcribe_provider": "openai", # openai | groq | openrouter
"transcribe_provider": "openai", # a key of TRANSCRIBERS
"transcribe_model": "gpt-4o-transcribe", # used when provider is openai
"groq_transcribe_model": "whisper-large-v3-turbo",
"openrouter_transcribe_model": "openai/gpt-4o-transcribe",
@@ -442,6 +443,22 @@ LEGACY_PROMPTS = {
"154fc5aca1166f00eebda705f848f0391bfbf5fe", # 1.2 English
}
# Every provider speech to text can run on, and the four settings that describe
# one. A fifth is a row here rather than another branch in transcribe_target(),
# another key row in the settings window and another line in save and load. The
# order is the order the provider box offers them in. `service` is the name the
# user sees; the environment variable that stands in for an empty key is the
# name of its setting, shouted.
Transcriber = collections.namedtuple("Transcriber", "service key url model")
TRANSCRIBERS = {
"openai": Transcriber("OpenAI", "openai_api_key", "openai_base_url",
"transcribe_model"),
"groq": Transcriber("Groq", "groq_api_key", "groq_base_url",
"groq_transcribe_model"),
"openrouter": Transcriber("OpenRouter", "openrouter_api_key",
"openrouter_base_url", "openrouter_transcribe_model"),
}
# Corners used to be stored with Turkish names.
_CORNER_MIGRATION = {
"sol-alt": "bottom-left", "sağ-alt": "bottom-right",
@@ -490,28 +507,27 @@ class Config:
def get(self, key, default=None):
return self.data.get(key, DEFAULTS.get(key, default))
def openai_key(self):
"""Fall back to the environment when no key is stored."""
return self["openai_api_key"].strip() or os.environ.get("OPENAI_API_KEY", "").strip()
def api_key(self, setting):
"""A stored key, or the environment variable that shares its name."""
return self[setting].strip() or os.environ.get(setting.upper(), "").strip()
def openrouter_key(self):
return self["openrouter_api_key"].strip() or os.environ.get("OPENROUTER_API_KEY", "").strip()
def openai_key(self):
return self.api_key("openai_api_key")
def groq_key(self):
return self["groq_api_key"].strip() or os.environ.get("GROQ_API_KEY", "").strip()
return self.api_key("groq_api_key")
def openrouter_key(self):
return self.api_key("openrouter_api_key")
def transcribe_target(self):
"""Key, endpoint and model for whichever provider does speech to text."""
if self["transcribe_provider"] == "openrouter":
return api.Target("openrouter", "OpenRouter", self.openrouter_key(),
self["openrouter_base_url"],
self["openrouter_transcribe_model"])
if self["transcribe_provider"] == "groq":
return api.Target("groq", "Groq", self.groq_key(),
self["groq_base_url"],
self["groq_transcribe_model"])
return api.Target("openai", "OpenAI", self.openai_key(),
self["openai_base_url"], self["transcribe_model"])
name = self["transcribe_provider"]
if name not in TRANSCRIBERS:
name = DEFAULTS["transcribe_provider"]
who = TRANSCRIBERS[name]
return api.Target(name, who.service, self.api_key(who.key),
self[who.url], self[who.model])
def cleanup_prompt(self, with_timestamps=False, with_speakers=False,
subtitles=False):