Spaces:
Sleeping
Sleeping
Slim Azure-only llm clients; remove description/preamble
Browse files- description.md +0 -27
- llm_query/llm_client.py +12 -66
- llm_query/span_marking/llm_client.py +5 -57
- preamble.md +0 -3
description.md
DELETED
|
@@ -1,27 +0,0 @@
|
|
| 1 |
-
## How it works
|
| 2 |
-
|
| 3 |
-
<p style="background-color: #fff9f9; border: 1px solid #ff0000; padding: 10px;">
|
| 4 |
-
Warning: This demo calls a live LLM API and is experimental.
|
| 5 |
-
</p>
|
| 6 |
-
|
| 7 |
-
Unlike embedding-based approaches that score token similarity directly, this demo compares two **generate-then-label** pipelines on the same input pair:
|
| 8 |
-
|
| 9 |
-
### Auxiliary synchronization
|
| 10 |
-
|
| 11 |
-
1. **Edit** — The LLM minimally edits Text A so it stays in A's language but becomes semantically closer to Text B (and vice versa).
|
| 12 |
-
2. **Diff** — A sequence alignment between the original and edited text yields binary labels per token.
|
| 13 |
-
3. **Highlight** — Edited tokens are shown in orange.
|
| 14 |
-
|
| 15 |
-
### Span marking
|
| 16 |
-
|
| 17 |
-
1. **Mark** — The LLM wraps dissimilar spans in Text A with `{{ }}` markers (without rewriting), and vice versa.
|
| 18 |
-
2. **Parse** — Markers are mapped onto tokens to yield binary labels.
|
| 19 |
-
3. **Highlight** — Marked tokens are shown in orange.
|
| 20 |
-
|
| 21 |
-
The inputs may be in different languages. When no difference is needed, the model responds with `pass`.
|
| 22 |
-
|
| 23 |
-
**Model:** `gpt-5.6-terra` (Azure, no reasoning) with prompt configs `config.v0.6.json` (auxiliary synchronization) and `config.span_marking.v0.6.json` (span marking).
|
| 24 |
-
|
| 25 |
-
More resources:
|
| 26 |
-
- Dataset: [ZurichNLP/SwissGov-RSD](https://huggingface.co/datasets/ZurichNLP/SwissGov-RSD)
|
| 27 |
-
- Related work: [Unsupervised Semantic Diff](https://huggingface.co/spaces/ZurichNLP/unsupervised-semantic-diff) (embedding-based)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
llm_query/llm_client.py
CHANGED
|
@@ -23,17 +23,17 @@ class LLMResponse:
|
|
| 23 |
response_obj: dict
|
| 24 |
|
| 25 |
|
| 26 |
-
class
|
| 27 |
|
| 28 |
-
|
| 29 |
-
API_KEY_NAME = "CSCS_SERVING_API"
|
| 30 |
|
| 31 |
-
def __init__(
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
self.config_name = config_name
|
| 38 |
with open(Path(__file__).parent / "configs" / config_name) as f:
|
| 39 |
self.config = json.load(f)
|
|
@@ -45,6 +45,7 @@ class LLMClient:
|
|
| 45 |
assert "edited_text_a" in example
|
| 46 |
|
| 47 |
self.model_name = model_name
|
|
|
|
| 48 |
self.cache_directory = cache_directory or Path(__file__).parent / ".llm_cache"
|
| 49 |
self.client = openai.Client(
|
| 50 |
api_key=os.environ.get(self.API_KEY_NAME),
|
|
@@ -65,7 +66,7 @@ class LLMClient:
|
|
| 65 |
return (self.model_name, self.config_name, text_a, text_b)
|
| 66 |
|
| 67 |
def _completion_extra_kwargs(self) -> dict:
|
| 68 |
-
kwargs = {}
|
| 69 |
for key in ("seed", "temperature"):
|
| 70 |
if key in self.config:
|
| 71 |
kwargs[key] = self.config[key]
|
|
@@ -95,10 +96,7 @@ class LLMClient:
|
|
| 95 |
})
|
| 96 |
return messages
|
| 97 |
|
| 98 |
-
def query(self,
|
| 99 |
-
text_a: str,
|
| 100 |
-
text_b: str,
|
| 101 |
-
) -> LLMResponse:
|
| 102 |
cache_key = self._get_cache_key(text_a, text_b)
|
| 103 |
|
| 104 |
if self.cache is not None and cache_key in self.cache:
|
|
@@ -143,55 +141,3 @@ class LLMClient:
|
|
| 143 |
return llm_response
|
| 144 |
llm_response.edited_text_a = self._normalize_edited_text(content, text_a)
|
| 145 |
return llm_response
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
class GPTClient(LLMClient):
|
| 149 |
-
|
| 150 |
-
BASE_URL = None # Default
|
| 151 |
-
API_KEY_NAME = "OPENAI_API_KEY"
|
| 152 |
-
|
| 153 |
-
def _completion_extra_kwargs(self) -> dict:
|
| 154 |
-
return {
|
| 155 |
-
"reasoning_effort": "none",
|
| 156 |
-
**super()._completion_extra_kwargs(),
|
| 157 |
-
}
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
class GeminiClient(LLMClient):
|
| 161 |
-
|
| 162 |
-
BASE_URL = "http://172.23.205.120:4000/v1"
|
| 163 |
-
API_KEY_NAME = "LITELLM_API_KEY"
|
| 164 |
-
|
| 165 |
-
def _completion_extra_kwargs(self) -> dict:
|
| 166 |
-
return {
|
| 167 |
-
"reasoning_effort": "minimal",
|
| 168 |
-
**super()._completion_extra_kwargs(),
|
| 169 |
-
}
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
class AzureClient(LLMClient):
|
| 173 |
-
|
| 174 |
-
API_KEY_NAME = "AZURE_API_KEY"
|
| 175 |
-
|
| 176 |
-
def __init__(self, *args, **kwargs):
|
| 177 |
-
self.BASE_URL = os.environ.get("AZURE_BASE_URL")
|
| 178 |
-
super().__init__(*args, **kwargs)
|
| 179 |
-
|
| 180 |
-
def _completion_extra_kwargs(self) -> dict:
|
| 181 |
-
return {
|
| 182 |
-
"reasoning_effort": "none",
|
| 183 |
-
**super()._completion_extra_kwargs(),
|
| 184 |
-
}
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
class DeepseekClient(LLMClient):
|
| 188 |
-
|
| 189 |
-
BASE_URL = "https://api.deepseek.com"
|
| 190 |
-
API_KEY_NAME = "DEEPSEEK_API_KEY"
|
| 191 |
-
|
| 192 |
-
def _completion_extra_kwargs(self) -> dict:
|
| 193 |
-
return {
|
| 194 |
-
"reasoning_effort": "high",
|
| 195 |
-
"extra_body": {"thinking": {"type": "enabled"}},
|
| 196 |
-
**super()._completion_extra_kwargs(),
|
| 197 |
-
}
|
|
|
|
| 23 |
response_obj: dict
|
| 24 |
|
| 25 |
|
| 26 |
+
class AzureClient:
|
| 27 |
|
| 28 |
+
API_KEY_NAME = "AZURE_API_KEY"
|
|
|
|
| 29 |
|
| 30 |
+
def __init__(
|
| 31 |
+
self,
|
| 32 |
+
model_name: str = "gpt-5.6-terra",
|
| 33 |
+
config_name: str = "config.v0.6.json",
|
| 34 |
+
cache_directory: Optional[Path] = None,
|
| 35 |
+
use_cache: bool = True,
|
| 36 |
+
):
|
| 37 |
self.config_name = config_name
|
| 38 |
with open(Path(__file__).parent / "configs" / config_name) as f:
|
| 39 |
self.config = json.load(f)
|
|
|
|
| 45 |
assert "edited_text_a" in example
|
| 46 |
|
| 47 |
self.model_name = model_name
|
| 48 |
+
self.BASE_URL = os.environ.get("AZURE_BASE_URL")
|
| 49 |
self.cache_directory = cache_directory or Path(__file__).parent / ".llm_cache"
|
| 50 |
self.client = openai.Client(
|
| 51 |
api_key=os.environ.get(self.API_KEY_NAME),
|
|
|
|
| 66 |
return (self.model_name, self.config_name, text_a, text_b)
|
| 67 |
|
| 68 |
def _completion_extra_kwargs(self) -> dict:
|
| 69 |
+
kwargs = {"reasoning_effort": "none"}
|
| 70 |
for key in ("seed", "temperature"):
|
| 71 |
if key in self.config:
|
| 72 |
kwargs[key] = self.config[key]
|
|
|
|
| 96 |
})
|
| 97 |
return messages
|
| 98 |
|
| 99 |
+
def query(self, text_a: str, text_b: str) -> LLMResponse:
|
|
|
|
|
|
|
|
|
|
| 100 |
cache_key = self._get_cache_key(text_a, text_b)
|
| 101 |
|
| 102 |
if self.cache is not None and cache_key in self.cache:
|
|
|
|
| 141 |
return llm_response
|
| 142 |
llm_response.edited_text_a = self._normalize_edited_text(content, text_a)
|
| 143 |
return llm_response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
llm_query/span_marking/llm_client.py
CHANGED
|
@@ -26,14 +26,13 @@ class SpanMarkingLLMResponse:
|
|
| 26 |
response_obj: dict
|
| 27 |
|
| 28 |
|
| 29 |
-
class
|
| 30 |
|
| 31 |
-
|
| 32 |
-
API_KEY_NAME = "CSCS_SERVING_API"
|
| 33 |
|
| 34 |
def __init__(
|
| 35 |
self,
|
| 36 |
-
model_name: str = "
|
| 37 |
config_name: str = "config.span_marking.v0.6.json",
|
| 38 |
cache_directory: Optional[Path] = None,
|
| 39 |
use_cache: bool = True,
|
|
@@ -49,6 +48,7 @@ class SpanMarkingLLMClient:
|
|
| 49 |
assert "marked_text_a" in example
|
| 50 |
|
| 51 |
self.model_name = model_name
|
|
|
|
| 52 |
self.cache_directory = cache_directory or Path(__file__).parent / ".llm_cache"
|
| 53 |
self.client = openai.Client(
|
| 54 |
api_key=os.environ.get(self.API_KEY_NAME),
|
|
@@ -69,7 +69,7 @@ class SpanMarkingLLMClient:
|
|
| 69 |
return (self.model_name, self.config_name, text_a, text_b)
|
| 70 |
|
| 71 |
def _completion_extra_kwargs(self) -> dict:
|
| 72 |
-
kwargs = {}
|
| 73 |
for key in ("seed", "temperature"):
|
| 74 |
if key in self.config:
|
| 75 |
kwargs[key] = self.config[key]
|
|
@@ -151,55 +151,3 @@ class SpanMarkingLLMClient:
|
|
| 151 |
return llm_response
|
| 152 |
llm_response.marked_text_a = self._normalize_marked_text(content, text_a)
|
| 153 |
return llm_response
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
class GPTClient(SpanMarkingLLMClient):
|
| 157 |
-
|
| 158 |
-
BASE_URL = None
|
| 159 |
-
API_KEY_NAME = "OPENAI_API_KEY"
|
| 160 |
-
|
| 161 |
-
def _completion_extra_kwargs(self) -> dict:
|
| 162 |
-
return {
|
| 163 |
-
"reasoning_effort": "none",
|
| 164 |
-
**super()._completion_extra_kwargs(),
|
| 165 |
-
}
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
class GeminiClient(SpanMarkingLLMClient):
|
| 169 |
-
|
| 170 |
-
BASE_URL = "http://172.23.205.120:4000/v1"
|
| 171 |
-
API_KEY_NAME = "LITELLM_API_KEY"
|
| 172 |
-
|
| 173 |
-
def _completion_extra_kwargs(self) -> dict:
|
| 174 |
-
return {
|
| 175 |
-
"reasoning_effort": "minimal",
|
| 176 |
-
**super()._completion_extra_kwargs(),
|
| 177 |
-
}
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
class AzureClient(SpanMarkingLLMClient):
|
| 181 |
-
|
| 182 |
-
API_KEY_NAME = "AZURE_API_KEY"
|
| 183 |
-
|
| 184 |
-
def __init__(self, *args, **kwargs):
|
| 185 |
-
self.BASE_URL = os.environ.get("AZURE_BASE_URL")
|
| 186 |
-
super().__init__(*args, **kwargs)
|
| 187 |
-
|
| 188 |
-
def _completion_extra_kwargs(self) -> dict:
|
| 189 |
-
return {
|
| 190 |
-
"reasoning_effort": "none",
|
| 191 |
-
**super()._completion_extra_kwargs(),
|
| 192 |
-
}
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
class DeepseekClient(SpanMarkingLLMClient):
|
| 196 |
-
|
| 197 |
-
BASE_URL = "https://api.deepseek.com"
|
| 198 |
-
API_KEY_NAME = "DEEPSEEK_API_KEY"
|
| 199 |
-
|
| 200 |
-
def _completion_extra_kwargs(self) -> dict:
|
| 201 |
-
return {
|
| 202 |
-
"reasoning_effort": "high",
|
| 203 |
-
"extra_body": {"thinking": {"type": "enabled"}},
|
| 204 |
-
**super()._completion_extra_kwargs(),
|
| 205 |
-
}
|
|
|
|
| 26 |
response_obj: dict
|
| 27 |
|
| 28 |
|
| 29 |
+
class AzureClient:
|
| 30 |
|
| 31 |
+
API_KEY_NAME = "AZURE_API_KEY"
|
|
|
|
| 32 |
|
| 33 |
def __init__(
|
| 34 |
self,
|
| 35 |
+
model_name: str = "gpt-5.6-terra",
|
| 36 |
config_name: str = "config.span_marking.v0.6.json",
|
| 37 |
cache_directory: Optional[Path] = None,
|
| 38 |
use_cache: bool = True,
|
|
|
|
| 48 |
assert "marked_text_a" in example
|
| 49 |
|
| 50 |
self.model_name = model_name
|
| 51 |
+
self.BASE_URL = os.environ.get("AZURE_BASE_URL")
|
| 52 |
self.cache_directory = cache_directory or Path(__file__).parent / ".llm_cache"
|
| 53 |
self.client = openai.Client(
|
| 54 |
api_key=os.environ.get(self.API_KEY_NAME),
|
|
|
|
| 69 |
return (self.model_name, self.config_name, text_a, text_b)
|
| 70 |
|
| 71 |
def _completion_extra_kwargs(self) -> dict:
|
| 72 |
+
kwargs = {"reasoning_effort": "none"}
|
| 73 |
for key in ("seed", "temperature"):
|
| 74 |
if key in self.config:
|
| 75 |
kwargs[key] = self.config[key]
|
|
|
|
| 151 |
return llm_response
|
| 152 |
llm_response.marked_text_a = self._normalize_marked_text(content, text_a)
|
| 153 |
return llm_response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
preamble.md
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
# Generative semantic diff
|
| 2 |
-
|
| 3 |
-
Demo for **generative semantic difference recognition**: compare auxiliary synchronization (LLM minimal edit, then sequence diff) and span marking (LLM `{{ }}` markers) on the same parallel texts.
|
|
|
|
|
|
|
|
|
|
|
|