GraphOfThoughts¶
cogitator.strategies.graph_of_thoughts
¶
Implements the Graph of Thoughts (GoT) framework.
AggregateOp
¶
Bases: GoTOperation
Aggregates multiple thoughts into new ones.
Source code in cogitator/strategies/graph_of_thoughts.py
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 |
|
execute_async(grs, llm, prompts, embedder, semaphore, trace=None, **global_kwargs)
async
¶
Aggregates thoughts asynchronously.
Source code in cogitator/strategies/graph_of_thoughts.py
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 |
|
GenerateOp
¶
Bases: GoTOperation
Generates new thoughts based on parent nodes.
Source code in cogitator/strategies/graph_of_thoughts.py
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 |
|
execute_async(grs, llm, prompts, embedder, semaphore, trace=None, **global_kwargs)
async
¶
Generates new thoughts asynchronously.
Source code in cogitator/strategies/graph_of_thoughts.py
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 |
|
GoTNode
¶
Represents a node (thought) in the Graph of Thoughts.
Source code in cogitator/strategies/graph_of_thoughts.py
__init__(steps, embedder=None, parents=None, data=None, text_content=None)
¶
Initializes a GoT node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
steps
|
List[str]
|
The sequence of reasoning steps/operations leading to this node. |
required |
embedder
|
Optional[BaseEmbedder]
|
The embedding model used for calculating node similarity. Can be None. |
None
|
parents
|
Optional[List[GoTNode]]
|
A list of parent nodes. |
None
|
data
|
Optional[Any]
|
Optional arbitrary data associated with the node. |
None
|
text_content
|
Optional[str]
|
Optional string representing the actual thought content. |
None
|
Source code in cogitator/strategies/graph_of_thoughts.py
__repr__()
¶
Returns a string representation of the node.
Source code in cogitator/strategies/graph_of_thoughts.py
is_ancestor(potential_ancestor)
¶
Checks if potential_ancestor
is an ancestor of this node.
Source code in cogitator/strategies/graph_of_thoughts.py
GoTOperation
¶
Bases: ABC
Abstract base class for all Graph of Thoughts operations.
Source code in cogitator/strategies/graph_of_thoughts.py
__init__(**params)
¶
execute(grs, llm, prompts, embedder=None, **global_kwargs)
abstractmethod
¶
Executes the operation, modifying the GraphReasoningState.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grs
|
GraphReasoningState
|
The current graph reasoning state. |
required |
llm
|
BaseLLM
|
The language model instance. |
required |
prompts
|
Dict[str, str]
|
A dictionary of available prompt templates. |
required |
embedder
|
Optional[BaseEmbedder]
|
Optional embedder for operations needing similarity. |
None
|
global_kwargs
|
Any
|
Global arguments like seed, max_tokens passed to LLM calls. |
{}
|
Source code in cogitator/strategies/graph_of_thoughts.py
execute_async(grs, llm, prompts, embedder=None, semaphore=None, trace=None, **global_kwargs)
abstractmethod
async
¶
Asynchronously executes the operation.
Source code in cogitator/strategies/graph_of_thoughts.py
GraphOfThoughts
¶
Implements the Graph of Thoughts (GoT) prompting framework.
GoT represents the reasoning process as a graph where nodes are partial solutions (thoughts) and edges represent dependencies or transformations between them. It allows for applying operations like generation, aggregation, scoring, and selection according to a defined Graph of Operations (GoO).
Reference
Besta et al. (v4; 2024) "Graph of Thoughts: Solving Elaborate Problems with Large Language Models". https://arxiv.org/abs/2308.09687
Source code in cogitator/strategies/graph_of_thoughts.py
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 807 808 809 810 |
|
__init__(llm, embedder=None, final_answer_format='text', prompts=None, max_tokens=None, seed=None)
¶
Initializes the GraphOfThoughts strategy handler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
llm
|
BaseLLM
|
The language model instance. |
required |
embedder
|
Optional[BaseEmbedder]
|
Optional embedding model instance for similarity checks. |
None
|
final_answer_format
|
Literal['text', 'json', 'direct_content']
|
Whether to extract the final answer as raw text, JSON, or directly from the best node content. |
'text'
|
prompts
|
Optional[Dict[str, str]]
|
A dictionary mapping operation types (e.g., 'expand', 'evaluate', 'aggregate', 'improve') to their prompt templates. |
None
|
max_tokens
|
Optional[int]
|
Default maximum tokens for LLM generation calls. |
None
|
seed
|
Optional[int]
|
Default random seed for LLM calls. |
None
|
Source code in cogitator/strategies/graph_of_thoughts.py
run(question, graph_of_operations, **kwargs)
¶
Synchronous execution is not supported for GraphOfThoughts.
Source code in cogitator/strategies/graph_of_thoughts.py
run_async(question, graph_of_operations, custom_operations=None, semaphore=None, with_trace=False, **kwargs)
async
¶
Asynchronously executes the Graph of Thoughts reasoning process based on a GoO.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
question
|
str
|
The initial question or problem statement. |
required |
graph_of_operations
|
List[Tuple[str, Dict]]
|
A list defining the sequence of operations and their parameters. Example: [('Generate', {'k': 5, 'output_set': 'thoughts1'}), ('Score', {'target_set': 'thoughts1'}), ('KeepBest', {'N': 3, 'target_set': 'thoughts1', 'output_set': 'frontier'}), ('Aggregate', {'target_sets': ['frontier'], 'k': 1, 'output_set': 'aggregated'}), ...] |
required |
custom_operations
|
Optional[Dict[str, GoTOperation]]
|
An optional dictionary mapping names to custom GoTOperation classes. |
None
|
semaphore
|
Optional[Semaphore]
|
Optional asyncio.Semaphore to limit concurrent LLM calls. |
None
|
with_trace
|
bool
|
If True, returns a tuple of (answer, trace). |
False
|
**kwargs
|
Any
|
Additional arguments passed to internal LLM calls (e.g., seed, max_tokens). |
{}
|
Returns:
Type | Description |
---|---|
Union[str, Tuple[str, Trace]]
|
The final answer string, or a tuple (answer, trace) if with_trace is True. |
Source code in cogitator/strategies/graph_of_thoughts.py
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 |
|
GraphReasoningState
¶
Maintains the dynamic state of the GoT reasoning process.
Source code in cogitator/strategies/graph_of_thoughts.py
__init__(root_node)
¶
Initializes the Graph Reasoning State.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
root_node
|
GoTNode
|
The initial node containing the problem input. |
required |
Source code in cogitator/strategies/graph_of_thoughts.py
add_node(node)
¶
Adds a new node to the state.
Source code in cogitator/strategies/graph_of_thoughts.py
get_active_set(set_name)
¶
get_nodes(node_ids)
¶
set_active_set(set_name, nodes)
¶
update_node(node)
¶
Updates an existing node in the state (e.g., score, validity).
Source code in cogitator/strategies/graph_of_thoughts.py
KeepBestOp
¶
Bases: GoTOperation
Selects the top N nodes based on score.
Source code in cogitator/strategies/graph_of_thoughts.py
execute(grs, llm, prompts, embedder, trace=None, **global_kwargs)
¶
Selects best nodes.
Source code in cogitator/strategies/graph_of_thoughts.py
execute_async(grs, llm, prompts, embedder, semaphore, trace=None, **global_kwargs)
async
¶
Selects best nodes (synchronous logic sufficient).
Source code in cogitator/strategies/graph_of_thoughts.py
ScoreOp
¶
Bases: GoTOperation
Scores thoughts using the LLM.
Source code in cogitator/strategies/graph_of_thoughts.py
execute_async(grs, llm, prompts, embedder, semaphore, trace=None, **global_kwargs)
async
¶
Scores nodes asynchronously.