LLM Connectivity Reference
Here we handle connections to various LLM services, proprietary and open source.
Handle connections to different LLM providers.
AnthropicConversation
Bases: Conversation
Conversation class for the Anthropic model.
Source code in biochatter/biochatter/llm_connect/anthropic.py
9 10 11 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 |
|
__init__(model_name, prompts, correct=False, split_correction=False)
Connect to Anthropic's API and set up a conversation with the user.
Also initialise a second conversational agent to provide corrections to the model output, if necessary.
model_name (str): The name of the model to use.
prompts (dict): A dictionary of prompts to use for the conversation.
split_correction (bool): Whether to correct the model output by
splitting the output into sentences and correcting each
sentence individually.
Source code in biochatter/biochatter/llm_connect/anthropic.py
set_api_key(api_key, user=None)
Set the API key for the Anthropic API.
If the key is valid, initialise the conversational agent. Optionally set the user for usage statistics.
api_key (str): The API key for the Anthropic API.
user (str, optional): The user for usage statistics. If provided and
equals "community", will track usage stats.
bool: True if the API key is valid, False otherwise.
Source code in biochatter/biochatter/llm_connect/anthropic.py
AzureGptConversation
Bases: GptConversation
Conversation class for the Azure GPT model.
Source code in biochatter/biochatter/llm_connect/azure.py
10 11 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 |
|
__init__(deployment_name, model_name, prompts, correct=False, split_correction=False, version=None, base_url=None, update_token_usage=None)
Connect to Azure's GPT API and set up a conversation with the user.
Extends GptConversation.
deployment_name (str): The name of the Azure deployment to use.
model_name (str): The name of the model to use. This is distinct
from the deployment name.
prompts (dict): A dictionary of prompts to use for the conversation.
correct (bool): Whether to correct the model output.
split_correction (bool): Whether to correct the model output by
splitting the output into sentences and correcting each
sentence individually.
version (str): The version of the Azure API to use.
base_url (str): The base URL of the Azure API to use.
update_token_usage (Callable): A function to update the token usage
statistics.
Source code in biochatter/biochatter/llm_connect/azure.py
set_api_key(api_key, user=None)
Set the API key for the Azure API.
If the key is valid, initialise the conversational agent. No user stats on Azure.
api_key (str): The API key for the Azure API.
user (str, optional): The user for usage statistics.
bool: True if the API key is valid, False otherwise.
Source code in biochatter/biochatter/llm_connect/azure.py
BloomConversation
Bases: Conversation
Conversation class for the Bloom model.
Source code in biochatter/biochatter/llm_connect/misc.py
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 |
|
__init__(model_name, prompts, split_correction)
Initialise the BloomConversation class.
DEPRECATED: Superceded by XinferenceConversation.
Source code in biochatter/biochatter/llm_connect/misc.py
set_api_key(api_key, user=None)
Set the API key for the HuggingFace API.
If the key is valid, initialise the conversational agent.
api_key (str): The API key for the HuggingFace API.
user (str): The user for usage statistics.
bool: True if the API key is valid, False otherwise.
Source code in biochatter/biochatter/llm_connect/misc.py
Conversation
Bases: ABC
Use this class to set up a connection to an LLM API.
Can be used to set the user name and API key, append specific messages for system, user, and AI roles (if available), set up the general context as well as manual and tool-based data inputs, and finally to query the API with prompts made by the user.
The conversation class is expected to have a messages
attribute to store
the conversation, and a history
attribute, which is a list of messages in
a specific format for logging / printing.
Source code in biochatter/biochatter/llm_connect/conversation.py
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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 |
|
ca_chat
property
writable
Access the correcting agent chat attribute with error handling.
chat
property
writable
Access the chat attribute with error handling.
use_ragagent_selector
property
writable
Whether to use the ragagent selector.
append_ai_message(message)
Add a message from the AI to the conversation.
message (str): The message from the AI.
Source code in biochatter/biochatter/llm_connect/conversation.py
append_ca_message(message)
Add a message to the correcting agent conversation.
message (str): The message to the correcting agent.
Source code in biochatter/biochatter/llm_connect/conversation.py
append_image_message(message, image_url, local=False)
Add a user message with an image to the conversation.
Also checks, in addition to the local
flag, if the image URL is a
local file path. If it is local, the image will be encoded as a base64
string to be passed to the LLM.
message (str): The message from the user.
image_url (str): The URL of the image.
local (bool): Whether the image is local or not. If local, it will
be encoded as a base64 string to be passed to the LLM.
Source code in biochatter/biochatter/llm_connect/conversation.py
append_system_message(message)
Add a system message to the conversation.
message (str): The system message.
Source code in biochatter/biochatter/llm_connect/conversation.py
append_user_message(message)
Add a message from the user to the conversation.
message (str): The message from the user.
Source code in biochatter/biochatter/llm_connect/conversation.py
bind_tools(tools)
Bind tools to the chat.
Source code in biochatter/biochatter/llm_connect/conversation.py
find_rag_agent(mode)
Find the rag_agent with the given mode.
get_last_injected_context()
Get a formatted list of the last context.
Get the last context injected into the conversation. Contains one dictionary for each RAG mode.
Returns
List[dict]: A list of dictionaries containing the mode and context
for each RAG agent.
Source code in biochatter/biochatter/llm_connect/conversation.py
get_msg_json()
Return a JSON representation of the conversation.
Returns a list of dicts of the messages in the conversation in JSON format. The keys of the dicts are the roles, the values are the messages.
Returns
str: A JSON representation of the messages in the conversation.
Source code in biochatter/biochatter/llm_connect/conversation.py
get_prompts()
query(text, image_url=None, structured_model=None, wrap_structured_output=None, tools=None, explain_tool_result=None, additional_tools_instructions=None, general_instructions_tool_interpretation=None, additional_instructions_tool_interpretation=None, mcp=None, return_tool_calls_as_ai_message=None, **kwargs)
Query the LLM API using the user's query.
Appends the most recent query to the conversation, optionally injects context from the RAG agent, and runs the primary query method of the child class.
text (str): The user query.
image_url (str): The URL of an image to include in the conversation.
Optional and only supported for models with vision capabilities.
structured_model (BaseModel): The structured output model to use for the query.
wrap_structured_output (bool): Whether to wrap the structured output in JSON quotes.
tools (list[Callable]): The tools to use for the query.
explain_tool_result (bool): Whether to explain the tool result.
additional_tools_instructions (str): The additional instructions for the query.
Mainly used for tools that do not support tool calling.
general_instructions_tool_interpretation (str): The general
instructions for the tool interpretation.
Overrides the default prompt in `GENERAL_TOOL_RESULT_INTERPRETATION_PROMPT`.
additional_instructions_tool_interpretation (str): The additional
instructions for the tool interpretation.
Overrides the default prompt in `ADDITIONAL_TOOL_RESULT_INTERPRETATION_PROMPT`.
mcp (bool): If you want to use MCP mode, this should be set to True.
return_tool_calls_as_ai_message (bool): If you want to return the tool calls as an AI message, this should be set to True.
**kwargs: Additional keyword arguments.
tuple: A tuple containing the response from the API, the token usage
information, and the correction if necessary/desired.
Source code in biochatter/biochatter/llm_connect/conversation.py
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 |
|
reset()
set_api_key(api_key, user=None)
abstractmethod
set_prompts(prompts)
set_rag_agent(agent)
Update or insert rag_agent.
If the rag_agent with the same mode already exists, it will be updated. Otherwise, the new rag_agent will be inserted.
Source code in biochatter/biochatter/llm_connect/conversation.py
set_user_name(user_name)
setup(context)
Set up the conversation with general prompts and a context.
Source code in biochatter/biochatter/llm_connect/conversation.py
setup_data_input_manual(data_input)
Set up the data input manually.
Source code in biochatter/biochatter/llm_connect/conversation.py
setup_data_input_tool(df, input_file_name)
Set up the data input tool.
Source code in biochatter/biochatter/llm_connect/conversation.py
GeminiConversation
Bases: Conversation
Conversation class for the Google Gemini model.
Source code in biochatter/biochatter/llm_connect/gemini.py
11 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 |
|
__init__(model_name, prompts, correct=False, split_correction=False, tools=None, tool_call_mode='auto')
Initialise the GeminiConversation class.
Connect to Google's Gemini API and set up a conversation with the user. Also initialise a second conversational agent to provide corrections to the model output, if necessary.
model_name (str): The name of the model to use.
prompts (dict): A dictionary of prompts to use for the conversation.
correct (bool): Whether to correct the model output.
split_correction (bool): Whether to correct the model output by
splitting the output into sentences and correcting each
sentence individually.
tools (list[Callable]): List of tool functions to use with the model.
tool_call_mode (str): The mode to use for tool calls.
"auto": Automatically call tools.
"text": Only return text output of the tool call.
Source code in biochatter/biochatter/llm_connect/gemini.py
set_api_key(api_key, user=None)
Set the API key for the Google Gemini API.
If the key is valid, initialise the conversational agent. Optionally set the user for usage statistics.
api_key (str): The API key for the Google Gemini API.
user (str, optional): The user for usage statistics. If provided and
equals "community", will track usage stats.
bool: True if the API key is valid, False otherwise.
Source code in biochatter/biochatter/llm_connect/gemini.py
GptConversation
Bases: Conversation
Conversation class for the OpenAI GPT model.
Source code in biochatter/biochatter/llm_connect/openai.py
11 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 |
|
__init__(model_name, prompts, correct=False, split_correction=False, base_url=None, update_token_usage=None)
Connect to OpenAI's GPT API and set up a conversation with the user.
Also initialise a second conversational agent to provide corrections to the model output, if necessary.
model_name (str): The name of the model to use.
prompts (dict): A dictionary of prompts to use for the conversation.
split_correction (bool): Whether to correct the model output by
splitting the output into sentences and correcting each
sentence individually.
base_url (str): Optional OpenAI base_url value to use custom
endpoint URL instead of default
Source code in biochatter/biochatter/llm_connect/openai.py
set_api_key(api_key, user=None)
Set the API key for the OpenAI API.
If the key is valid, initialise the conversational agent. Optionally set the user for usage statistics.
api_key (str): The API key for the OpenAI API.
user (str, optional): The user for usage statistics. If provided and
equals "community", will track usage stats.
bool: True if the API key is valid, False otherwise.
Source code in biochatter/biochatter/llm_connect/openai.py
LangChainConversation
Bases: Conversation
Conversation class for a generic LangChain model.
Source code in biochatter/biochatter/llm_connect/langchain.py
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 |
|
__init__(model_name, model_provider, prompts, correct=False, split_correction=False, tools=None, tool_call_mode='auto', async_mode=False, mcp=False)
Initialise the LangChainConversation class.
Connect to a generic LangChain model and set up a conversation with the user. Also initialise a second conversational agent to provide corrections to the model output, if necessary.
model_name (str): The name of the model to use.
model_provider (str): The provider of the model to use.
prompts (dict): A dictionary of prompts to use for the conversation.
correct (bool): Whether to correct the model output.
split_correction (bool): Whether to correct the model output by
splitting the output into sentences and correcting each
sentence individually.
tools (list[Callable]): List of tool functions to use with the
model.
tool_call_mode (str): The mode to use for tool calls.
"auto": Automatically call tools.
"text": Only return text output of the tool call.
async_mode (bool): Whether to run in async mode. Defaults to False.
mcp (bool): If you want to use MCP mode, this should be set to True.
Source code in biochatter/biochatter/llm_connect/langchain.py
set_api_key(api_key=None, user=None)
Set the API key for the model provider.
If the key is valid, initialise the conversational agent. Optionally set the user for usage statistics.
api_key (str): The API key for the model provider.
user (str, optional): The user for usage statistics. If provided and
equals "community", will track usage stats.
bool: True if the API key is valid, False otherwise.
Source code in biochatter/biochatter/llm_connect/langchain.py
LiteLLMConversation
Bases: Conversation
A unified interface for multiple LLM models using LiteLLM.
This class implements the abstract methods from the Conversation parent class and provides a unified way to interact with different LLM providers through LiteLLM, which supports models from OpenAI, Anthropic, HuggingFace, and more.
Attributes:
Name | Type | Description |
---|---|---|
model_name |
str
|
The name of the model to use. |
prompts |
dict
|
Dictionary containing various prompts used in the conversation. |
correct |
bool
|
Whether to use a correcting agent. |
split_correction |
bool
|
Whether to split corrections by sentence. |
rag_agents |
list
|
List of RAG agents available for context enhancement. |
history |
list
|
Conversation history for logging/printing. |
messages |
list
|
Messages in the conversation. |
ca_messages |
list
|
Messages for the correcting agent. |
api_key |
str
|
API key for the LLM provider. |
user |
str
|
Username for the API, if required. |
Source code in biochatter/biochatter/llm_connect/llmlite.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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
|
__init__(model_name, prompts, correct=False, split_correction=False, use_ragagent_selector=False, update_token_usage=None)
Initialize a UnifiedConversation instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_name
|
str
|
The name of the model to use. |
required |
prompts
|
dict
|
Dictionary containing various prompts used in the conversation. |
required |
correct
|
bool
|
Whether to use a correcting agent. Defaults to False. |
False
|
split_correction
|
bool
|
Whether to split corrections by sentence. Defaults to False. |
False
|
use_ragagent_selector
|
bool
|
Whether to use RagAgentSelector. Defaults to False. |
False
|
update_token_usage
|
Callable
|
A function to update the token usage statistics. |
None
|
Source code in biochatter/biochatter/llm_connect/llmlite.py
get_all_model_info()
get_all_model_list()
get_litellm_object(api_key, model)
Get a LiteLLM object for the specified model and API key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
api_key
|
str
|
The API key for the LLM provider. |
required |
model
|
str
|
The name of the model to use. |
required |
Returns:
Name | Type | Description |
---|---|---|
ChatLiteLLM |
ChatLiteLLM
|
An instance of ChatLiteLLM configured with the specified model, temperature, max tokens and API key. |
Raises:
Type | Description |
---|---|
ValueError
|
If the API key is None. |
AuthenticationError
|
If there is an authentication error. |
InvalidRequestError
|
If the request is invalid. |
RateLimitError
|
If the rate limit is exceeded. |
ServiceUnavailableError
|
If the service is unavailable. |
APIError
|
If there is a general API error. |
Timeout
|
If the request times out. |
APIConnectionError
|
If there is a connection error. |
InternalServerError
|
If there is an internal server error. |
Exception
|
If there is an unexpected error. |
Source code in biochatter/biochatter/llm_connect/llmlite.py
get_model_info(model)
Get information about a specific model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
str
|
The name of the model. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
A dictionary containing information about the specified model. |
Source code in biochatter/biochatter/llm_connect/llmlite.py
get_model_max_tokens(model)
Get the maximum number of tokens for a specific model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
str
|
The name of the model. |
required |
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The maximum number of tokens for the specified model. |
Source code in biochatter/biochatter/llm_connect/llmlite.py
get_models_by_provider()
json_serializable(obj)
Convert non-serializable objects to serializable format.
Source code in biochatter/biochatter/llm_connect/llmlite.py
parse_llm_response(response)
Parse the response from the LLM.
Source code in biochatter/biochatter/llm_connect/llmlite.py
set_api_key(api_key, user=None)
Set the API key for the LLM provider.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
api_key
|
str
|
The API key for the LLM provider. |
required |
user
|
Union[str, None]
|
The username |
None
|
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the API key is successfully set, False otherwise. |
Raises:
Type | Description |
---|---|
ValueError
|
If the model name or correction model name is not set. |
TypeError
|
If the LiteLLM object initialization fails. |
Exception
|
If there is an unexpected error. |
Source code in biochatter/biochatter/llm_connect/llmlite.py
OllamaConversation
Bases: Conversation
Conversation class for the Ollama model.
Source code in biochatter/biochatter/llm_connect/ollama.py
8 9 10 11 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 |
|
__init__(base_url, prompts, model_name='llama3', correct=False, split_correction=False)
Connect to an Ollama LLM via the Ollama/Langchain library.
Set up a conversation with the user. Also initialise a second conversational agent to provide corrections to the model output, if necessary.
base_url (str): The base URL of the Ollama instance.
prompts (dict): A dictionary of prompts to use for the conversation.
model_name (str): The name of the model to use. Can be any model
name available in your Ollama instance.
correct (bool): Whether to correct the model output.
split_correction (bool): Whether to correct the model output by
splitting the output into sentences and correcting each sentence
individually.
Source code in biochatter/biochatter/llm_connect/ollama.py
append_ca_message(message)
Override the system message addition for the correcting agent.
Ollama does not accept multiple system messages. Concatenate them if there are multiple.
TODO this currently assumes that the correcting agent is the same model as the primary one.
message (str): The message to append.
Source code in biochatter/biochatter/llm_connect/ollama.py
append_system_message(message)
Override the system message addition.
Ollama does not accept multiple system messages. Concatenate them if there are multiple.
message (str): The message to append.
Source code in biochatter/biochatter/llm_connect/ollama.py
set_api_key(api_key, user=None)
Set the API key for the Ollama API. Not implemented.
api_key (str): The API key for the Ollama API.
user (str): The user for usage statistics.
bool: True if the API key is valid, False otherwise.
Source code in biochatter/biochatter/llm_connect/ollama.py
WasmConversation
Bases: Conversation
Conversation class for the wasm model.
Source code in biochatter/biochatter/llm_connect/misc.py
__init__(model_name, prompts, correct=False, split_correction=False)
Initialize the WasmConversation class.
This class is used to return the complete query as a string to be used
in the frontend running the wasm model. It does not call the API itself,
but updates the message history similarly to the other conversation
classes. It overrides the query
method from the Conversation
class
to return a plain string that contains the entire message for the model
as the first element of the tuple. The second and third elements are
None
as there is no token usage or correction for the wasm model.
Source code in biochatter/biochatter/llm_connect/misc.py
query(text)
Return the entire message history as a single string.
This is the message that is sent to the wasm model.
text (str): The user query.
tuple: A tuple containing the message history as a single string,
and `None` for the second and third elements of the tuple.
Source code in biochatter/biochatter/llm_connect/misc.py
XinferenceConversation
Bases: Conversation
Conversation class for the Xinference deployment.
Source code in biochatter/biochatter/llm_connect/xinference.py
7 8 9 10 11 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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
|
__init__(base_url, prompts, model_name='auto', correct=False, split_correction=False)
Connect to an open-source LLM via the Xinference client.
Connect to a running Xinference deployment and set up a conversation with the user. Also initialise a second conversational agent to provide corrections to the model output, if necessary.
base_url (str): The base URL of the Xinference instance (should not
include the /v1 part).
prompts (dict): A dictionary of prompts to use for the conversation.
model_name (str): The name of the model to use. Will be mapped to
the according uid from the list of available models. Can be set to
"auto" to use the first available model.
correct (bool): Whether to correct the model output.
split_correction (bool): Whether to correct the model output by
splitting the output into sentences and correcting each sentence
individually.
Source code in biochatter/biochatter/llm_connect/xinference.py
append_ca_message(message)
Override the system message addition for the correcting agent.
Xinference does not accept multiple system messages. We concatenate them if there are multiple.
TODO this currently assumes that the correcting agent is the same model as the primary one.
message (str): The message to append.
Source code in biochatter/biochatter/llm_connect/xinference.py
append_system_message(message)
Override the system message addition.
Xinference does not accept multiple system messages. We concatenate them if there are multiple.
message (str): The message to append.
Source code in biochatter/biochatter/llm_connect/xinference.py
list_models_by_type(model_type)
List the models by type.
model_type (str): The type of model to list.
list[str]: A list of model names.
Source code in biochatter/biochatter/llm_connect/xinference.py
load_models()
Load the models from the Xinference client.
set_api_key()
Try to get the Xinference model from the client API.
If the model is found, initialise the conversational agent. If the model
is not found, get_model
will raise a RuntimeError.
Returns
bool: True if the model is found, False otherwise.