llm_mapper
LlmMapper
Source code in src/ariadne/llm_mapping/llm_mapper.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | |
get_total_cost()
Returns the total cost incurred for LLM calls
Returns:
| Type | Description |
|---|---|
float
|
Total cost in USD. |
Source code in src/ariadne/llm_mapping/llm_mapper.py
263 264 265 266 267 268 269 270 271 | |
map_term(source_term, source_id, target_concepts, concept_id_column='matched_concept_id', concept_name_column='matched_concept_name', domain_id_column='matched_domain_id', concept_class_id_column='matched_concept_class_id', vocabulary_id_column='matched_vocabulary_id', parents_column='matched_parents', children_column='matched_children', synonyms_column='matched_synonyms')
Maps a source term to the matching target concept using LLM prompts. The LLM can be prompted in multiple steps. The first step provides the source term and candidate target concepts as prompt, with information specified in config.llm_mapping.context. Subsequent steps use the response from the previous step as prompt, unless config.llm_mapping.context.re_insert_target_details is set to True, in which case the target concept details are re-inserted into the response JSON for the next step.
Finally, the response is processed to extract the matched concept ID and name, looking for a line starting with
"Match:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_term
|
str
|
The source clinical term to map. |
required |
source_id
|
Optional[str]
|
An optional unique identifier for the source term, used for caching responses. |
required |
target_concepts
|
DataFrame
|
A DataFrame containing candidate target concepts with columns: |
required |
concept_id_column
|
str
|
The name of the column containing target concept IDs. |
'matched_concept_id'
|
concept_name_column
|
str
|
The name of the column containing target concept names. |
'matched_concept_name'
|
domain_id_column
|
Optional[str]
|
The name of the column containing target domain IDs. |
'matched_domain_id'
|
concept_class_id_column
|
Optional[str]
|
The name of the column containing target concept class IDs. |
'matched_concept_class_id'
|
vocabulary_id_column
|
Optional[str]
|
The name of the column containing target vocabulary IDs. |
'matched_vocabulary_id'
|
parents_column
|
Optional[str]
|
The name of the column containing target concept parents. |
'matched_parents'
|
children_column
|
Optional[str]
|
The name of the column containing target concept children. |
'matched_children'
|
synonyms_column
|
Optional[str]
|
The name of the column containing target concept synonyms. |
'matched_synonyms'
|
Returns:
| Type | Description |
|---|---|
int | None
|
A tuple of (matched_concept_id, matched_concept_name, match_rationale). If no match is found, returns |
str | None
|
(-1, "no_match", ""). If the content filter is hit, returns (None, None, None). |
Source code in src/ariadne/llm_mapping/llm_mapper.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | |
map_terms(source_target_concepts, term_column='cleaned_term', source_id_column='source_concept_id', source_term_column='source_term', concept_id_column='matched_concept_id', concept_name_column='matched_concept_name', domain_id_column='matched_domain_id', concept_class_id_column='matched_concept_class_id', vocabulary_id_column='matched_vocabulary_id', parents_column='matched_parents', children_column='matched_children', synonyms_column='matched_synonyms', mapped_concept_id_column='mapped_concept_id', mapped_concept_name_column='mapped_concept_name', mapped_rationale_column='mapped_rationale')
Maps source terms in a DataFrame column to target concepts using LLM prompts. The system prompts are taken from the configuration file. Multiple steps are supported as per the map_term method.
The input DataFrame should contain multiple rows per source term, one for each candidate target concept.
Be aware that LLM responses are cached based on source term and source ID, so if the same term appears multiple times with the same source ID, the cached response will be used. The cache is stored in the llm_mapper_responses_folder specified in the config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_target_concepts
|
DataFrame
|
DataFrame containing the source clinical terms and candidate target concepts. |
required |
term_column
|
str
|
The name of the column containing source terms fed to the LLM. |
'cleaned_term'
|
source_id_column
|
Optional[str]
|
The name of the column containing the unique source term IDs. |
'source_concept_id'
|
source_term_column
|
Optional[str]
|
The name of the column containing the original source terms. |
'source_term'
|
concept_id_column
|
str
|
The name of the column containing the target concept IDs. |
'matched_concept_id'
|
concept_name_column
|
str
|
The name of the column containing the target concept names. |
'matched_concept_name'
|
domain_id_column
|
Optional[str]
|
The name of the column containing the target domain IDs. |
'matched_domain_id'
|
concept_class_id_column
|
Optional[str]
|
The name of the column containing the target concept class IDs. |
'matched_concept_class_id'
|
vocabulary_id_column
|
Optional[str]
|
The name of the column containing the target vocabulary IDs. |
'matched_vocabulary_id'
|
parents_column
|
Optional[str]
|
The name of the column containing the target concept parents. |
'matched_parents'
|
children_column
|
Optional[str]
|
The name of the column containing the target concept children. |
'matched_children'
|
synonyms_column
|
Optional[str]
|
The name of the column containing the target concept synonyms. |
'matched_synonyms'
|
mapped_concept_id_column
|
str
|
The name of the output column for mapped concept IDs. |
'mapped_concept_id'
|
mapped_concept_name_column
|
str
|
The name of the output column for mapped concept names. |
'mapped_concept_name'
|
mapped_rationale_column
|
str
|
The name of the output column for mapping rationale. |
'mapped_rationale'
|
Returns: A DataFrame with the original terms and their mapped concept IDs and names.
Source code in src/ariadne/llm_mapping/llm_mapper.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |