fabricatio 0.2.6.dev3__tar.gz → 0.2.6.dev5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/PKG-INFO +1 -1
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/examples/extract_and_review/extract_and_review.py +2 -1
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/examples/extract_article/extract.py +2 -1
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/examples/make_diary/diary.py +1 -1
- fabricatio-0.2.6.dev5/examples/search_bibtex/search.py +11 -0
- fabricatio-0.2.6.dev5/examples/write_outline/write_outline_corrected.py +44 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/pyproject.toml +1 -1
- fabricatio-0.2.6.dev5/python/fabricatio/__init__.py +43 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/_rust.pyi +0 -1
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/actions/article.py +46 -2
- fabricatio-0.2.6.dev5/python/fabricatio/capabilities/covalidate.py +160 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/capabilities/task.py +2 -2
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/config.py +18 -15
- fabricatio-0.2.6.dev5/python/fabricatio/models/action.py +219 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/models/extra.py +4 -4
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/models/kwargs_types.py +2 -2
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/models/role.py +32 -7
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/models/usages.py +5 -4
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/parser.py +1 -0
- fabricatio-0.2.6.dev5/python/fabricatio/workflows/articles.py +26 -0
- fabricatio-0.2.6.dev5/templates/built-in/co_validation.hbs +26 -0
- fabricatio-0.2.6.dev5/templates.tar.gz +0 -0
- fabricatio-0.2.6.dev5/uv.lock +1549 -0
- fabricatio-0.2.6.dev3/examples/search_bibtex/search.py +0 -12
- fabricatio-0.2.6.dev3/python/fabricatio/__init__.py +0 -60
- fabricatio-0.2.6.dev3/python/fabricatio/models/action.py +0 -158
- fabricatio-0.2.6.dev3/python/fabricatio/workflows/articles.py +0 -15
- fabricatio-0.2.6.dev3/templates.tar.gz +0 -0
- fabricatio-0.2.6.dev3/uv.lock +0 -1547
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/.github/workflows/build-package.yaml +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/.github/workflows/ruff.yaml +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/.github/workflows/tests.yaml +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/.gitignore +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/.python-version +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/Cargo.lock +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/Cargo.toml +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/LICENSE +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/Makefile +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/README.md +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/examples/correct/correct.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/examples/correct/correct_loop.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/examples/extract_and_review/.gitignore +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/examples/llm_usages/llm_usage.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/examples/make_a_rating/rating.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/examples/make_diary/commits.json +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/examples/minor/hello_fabricatio.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/examples/minor/write_a_poem.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/examples/propose_task/propose.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/examples/reviewer/review.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/examples/search_bibtex/.gitignore +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/examples/simple_chat/chat.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/examples/simple_rag/simple_rag.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/examples/task_handle/handle_task.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/examples/write_outline/.gitignore +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/examples/write_outline/write_outline.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/_rust_instances.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/actions/output.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/actions/rag.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/capabilities/correct.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/capabilities/propose.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/capabilities/rag.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/capabilities/rating.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/capabilities/review.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/core.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/decorators.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/fs/__init__.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/fs/curd.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/fs/readers.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/journal.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/models/events.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/models/generic.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/models/task.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/models/tool.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/models/utils.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/py.typed +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/toolboxes/__init__.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/toolboxes/arithmetic.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/toolboxes/fs.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/python/fabricatio/workflows/rag.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/src/bib_tools.rs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/src/hash.rs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/src/hbs_helpers.rs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/src/lib.rs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/src/templates.rs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/binary-exploitation-ctf-solver.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/claude-xml.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/clean-up-code.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/correct.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/create_json_obj.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/cryptography-ctf-solver.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/dependencies.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/document-the-code.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/draft_rating_criteria.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/draft_rating_manual.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/draft_rating_weights_klee.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/draft_tool_usage_code.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/extract_criteria_from_reasons.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/extract_reasons_from_examples.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/find-security-vulnerabilities.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/fix-bugs.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/generic_string.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/improve-performance.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/liststr.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/make_choice.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/make_judgment.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/pathstr.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/rate_fine_grind.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/refactor.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/refined_query.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/retrieved_display.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/reverse-engineering-ctf-solver.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/review_string.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/task_briefing.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/web-ctf-solver.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/write-git-commit.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/write-github-pull-request.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/templates/built-in/write-github-readme.hbs +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/tests/test_config.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/tests/test_models/test_action.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/tests/test_models/test_advanced.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/tests/test_models/test_generic.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/tests/test_models/test_role.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/tests/test_models/test_task.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/tests/test_models/test_tool.py +0 -0
- {fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/tests/test_models/test_usages.py +0 -0
{fabricatio-0.2.6.dev3 → fabricatio-0.2.6.dev5}/examples/extract_and_review/extract_and_review.py
RENAMED
@@ -4,7 +4,8 @@ import asyncio
|
|
4
4
|
from pathlib import Path
|
5
5
|
from typing import List
|
6
6
|
|
7
|
-
from fabricatio import ArticleEssence, Event,
|
7
|
+
from fabricatio import ArticleEssence, Event, Role, Task, WorkFlow, logger
|
8
|
+
from fabricatio.actions.article import ExtractArticleEssence
|
8
9
|
from fabricatio.fs.curd import dump_text, gather_files
|
9
10
|
|
10
11
|
|
@@ -3,7 +3,8 @@
|
|
3
3
|
import asyncio
|
4
4
|
from typing import List
|
5
5
|
|
6
|
-
from fabricatio import ArticleEssence, Event,
|
6
|
+
from fabricatio import ArticleEssence, Event, Role, Task, WorkFlow, logger
|
7
|
+
from fabricatio.actions.article import ExtractArticleEssence
|
7
8
|
|
8
9
|
|
9
10
|
async def main() -> None:
|
@@ -57,7 +57,7 @@ class DumpText(Action):
|
|
57
57
|
["dump the text contained in `text_to_dump` to a file", "only return the path of the written file"]
|
58
58
|
)
|
59
59
|
|
60
|
-
path = await self.
|
60
|
+
path = await self.handle_fine_grind(
|
61
61
|
task_input,
|
62
62
|
{"text_to_dump": dump_text},
|
63
63
|
)
|
@@ -0,0 +1,11 @@
|
|
1
|
+
"""Example of using BibManager."""
|
2
|
+
from fabricatio import BibManager, logger
|
3
|
+
|
4
|
+
b = BibManager("Exported Items.bib")
|
5
|
+
logger.success(
|
6
|
+
b.get_cite_key("A Negative Selection Immune System Inspired Methodology for Fault Diagnosis of Wind Turbines"))
|
7
|
+
logger.success(
|
8
|
+
b.get_cite_key_fuzzy(
|
9
|
+
"System Inspired Methodology for Fault"
|
10
|
+
)
|
11
|
+
)
|
@@ -0,0 +1,44 @@
|
|
1
|
+
"""Example of using the library."""
|
2
|
+
|
3
|
+
import asyncio
|
4
|
+
|
5
|
+
from fabricatio import Event, Role, WorkFlow, logger
|
6
|
+
from fabricatio.actions.article import CorrectOutline, CorrectProposal, GenerateArticleProposal, GenerateOutline
|
7
|
+
from fabricatio.actions.output import DumpFinalizedOutput
|
8
|
+
|
9
|
+
|
10
|
+
async def main() -> None:
|
11
|
+
"""Main function."""
|
12
|
+
role = Role(
|
13
|
+
name="Undergraduate Researcher",
|
14
|
+
description="Write an outline for an article in typst format.",
|
15
|
+
llm_top_p=0.8,
|
16
|
+
llm_temperature=1.15,
|
17
|
+
registry={
|
18
|
+
Event.quick_instantiate(ns := "article"): WorkFlow(
|
19
|
+
name="Generate Article Outline",
|
20
|
+
description="Generate an outline for an article. dump the outline to the given path. in typst format.",
|
21
|
+
steps=(
|
22
|
+
GenerateArticleProposal(llm_model="deepseek/deepseek-reasoner", llm_temperature=1.3),
|
23
|
+
CorrectProposal(
|
24
|
+
output_key="article_proposal",
|
25
|
+
llm_model="deepseek/deepseek-reasoner",
|
26
|
+
llm_temperature=1.3,
|
27
|
+
),
|
28
|
+
GenerateOutline(llm_model="deepseek/deepseek-chat", llm_temperature=1.4, llm_top_p=0.5),
|
29
|
+
CorrectOutline(output_key="to_dump", llm_temperature=1.4, llm_top_p=0.45),
|
30
|
+
DumpFinalizedOutput(output_key="task_output"),
|
31
|
+
),
|
32
|
+
)
|
33
|
+
},
|
34
|
+
)
|
35
|
+
|
36
|
+
proposed_task = await role.propose_task(
|
37
|
+
"You need to read the `./article_briefing.txt` file and write an outline for the article in typst format. The outline should be saved in the `./out.typ` file.",
|
38
|
+
)
|
39
|
+
path = await proposed_task.delegate(ns)
|
40
|
+
logger.success(f"The outline is saved in:\n{path}")
|
41
|
+
|
42
|
+
|
43
|
+
if __name__ == "__main__":
|
44
|
+
asyncio.run(main())
|
@@ -0,0 +1,43 @@
|
|
1
|
+
"""Fabricatio is a Python library for building llm app using event-based agent structure."""
|
2
|
+
|
3
|
+
from importlib.util import find_spec
|
4
|
+
|
5
|
+
from fabricatio import actions, toolboxes, workflows
|
6
|
+
from fabricatio._rust import BibManager
|
7
|
+
from fabricatio._rust_instances import TEMPLATE_MANAGER
|
8
|
+
from fabricatio.core import env
|
9
|
+
from fabricatio.journal import logger
|
10
|
+
from fabricatio.models import extra
|
11
|
+
from fabricatio.models.action import Action, WorkFlow
|
12
|
+
from fabricatio.models.events import Event
|
13
|
+
from fabricatio.models.role import Role
|
14
|
+
from fabricatio.models.task import Task
|
15
|
+
from fabricatio.models.tool import ToolBox
|
16
|
+
from fabricatio.parser import Capture, GenericCapture, JsonCapture, PythonCapture
|
17
|
+
|
18
|
+
__all__ = [
|
19
|
+
"TEMPLATE_MANAGER",
|
20
|
+
"Action",
|
21
|
+
"BibManager",
|
22
|
+
"Capture",
|
23
|
+
"Event",
|
24
|
+
"GenericCapture",
|
25
|
+
"JsonCapture",
|
26
|
+
"PythonCapture",
|
27
|
+
"Role",
|
28
|
+
"Task",
|
29
|
+
"ToolBox",
|
30
|
+
"WorkFlow",
|
31
|
+
"actions",
|
32
|
+
"env",
|
33
|
+
"extra",
|
34
|
+
"logger",
|
35
|
+
"toolboxes",
|
36
|
+
"workflows",
|
37
|
+
]
|
38
|
+
|
39
|
+
|
40
|
+
if find_spec("pymilvus"):
|
41
|
+
from fabricatio.capabilities.rag import RAG
|
42
|
+
|
43
|
+
__all__ += ["RAG"]
|
@@ -2,13 +2,15 @@
|
|
2
2
|
|
3
3
|
from os import PathLike
|
4
4
|
from pathlib import Path
|
5
|
-
from typing import Callable, List, Optional
|
5
|
+
from typing import Any, Callable, List, Optional
|
6
6
|
|
7
7
|
from fabricatio.fs import safe_text_read
|
8
8
|
from fabricatio.journal import logger
|
9
9
|
from fabricatio.models.action import Action
|
10
10
|
from fabricatio.models.extra import ArticleEssence, ArticleOutline, ArticleProposal
|
11
11
|
from fabricatio.models.task import Task
|
12
|
+
from questionary import confirm, text
|
13
|
+
from rich import print as rprint
|
12
14
|
|
13
15
|
|
14
16
|
class ExtractArticleEssence(Action):
|
@@ -66,7 +68,7 @@ class GenerateArticleProposal(Action):
|
|
66
68
|
class GenerateOutline(Action):
|
67
69
|
"""Generate the article based on the outline."""
|
68
70
|
|
69
|
-
output_key: str = "
|
71
|
+
output_key: str = "article_outline"
|
70
72
|
"""The key of the output data."""
|
71
73
|
|
72
74
|
async def _execute(
|
@@ -79,3 +81,45 @@ class GenerateOutline(Action):
|
|
79
81
|
article_proposal.display(),
|
80
82
|
system_message=f"# your personal briefing: \n{self.briefing}",
|
81
83
|
)
|
84
|
+
|
85
|
+
|
86
|
+
class CorrectProposal(Action):
|
87
|
+
"""Correct the proposal of the article."""
|
88
|
+
|
89
|
+
output_key: str = "corrected_proposal"
|
90
|
+
|
91
|
+
async def _execute(self, task_input: Task, article_proposal: ArticleProposal, **_) -> Any:
|
92
|
+
input_path = await self.awhich_pathstr(
|
93
|
+
f"{task_input.briefing}\nExtract the path of file, which contains the article briefing that I need to read."
|
94
|
+
)
|
95
|
+
|
96
|
+
rprint(article_proposal.display())
|
97
|
+
ret = None
|
98
|
+
while await confirm("Do you want to correct the Proposal?").ask_async():
|
99
|
+
topic = await text("What is the topic of the proposal?").ask_async()
|
100
|
+
ret = await self.correct_obj(
|
101
|
+
article_proposal,
|
102
|
+
safe_text_read(input_path),
|
103
|
+
topic=topic,
|
104
|
+
)
|
105
|
+
return ret or article_proposal
|
106
|
+
|
107
|
+
|
108
|
+
class CorrectOutline(Action):
|
109
|
+
"""Correct the outline of the article."""
|
110
|
+
|
111
|
+
output_key: str = "corrected_outline"
|
112
|
+
"""The key of the output data."""
|
113
|
+
|
114
|
+
async def _execute(
|
115
|
+
self,
|
116
|
+
article_outline: ArticleOutline,
|
117
|
+
article_proposal: ArticleProposal,
|
118
|
+
**_,
|
119
|
+
) -> Optional[str]:
|
120
|
+
rprint(article_outline.finalized_dump())
|
121
|
+
ret = None
|
122
|
+
while await confirm("Do you want to correct the outline?").ask_async():
|
123
|
+
topic = await text("What is the topic of the outline?").ask_async()
|
124
|
+
ret = await self.correct_obj(article_outline, article_proposal.display(), topic=topic)
|
125
|
+
return ret or article_outline
|
@@ -0,0 +1,160 @@
|
|
1
|
+
"""Co-validation capability for LLMs."""
|
2
|
+
|
3
|
+
from asyncio import gather
|
4
|
+
from typing import Callable, List, Optional, Union, Unpack, overload
|
5
|
+
|
6
|
+
from fabricatio import TEMPLATE_MANAGER
|
7
|
+
from fabricatio.config import configs
|
8
|
+
from fabricatio.journal import logger
|
9
|
+
from fabricatio.models.kwargs_types import GenerateKwargs
|
10
|
+
from fabricatio.models.usages import LLMUsage
|
11
|
+
|
12
|
+
|
13
|
+
class CoValidate(LLMUsage):
|
14
|
+
"""Class that represents a co-validation capability using multiple LLMs.
|
15
|
+
|
16
|
+
This class provides methods to validate responses by attempting multiple approaches:
|
17
|
+
1. Using the primary LLM to generate a response
|
18
|
+
2. Using a secondary (co-) model to refine responses that fail validation
|
19
|
+
3. Trying multiple times if needed
|
20
|
+
"""
|
21
|
+
|
22
|
+
@overload
|
23
|
+
async def aask_covalidate[T](
|
24
|
+
self,
|
25
|
+
question: str,
|
26
|
+
validator: Callable[[str], T | None],
|
27
|
+
co_model: Optional[str] = None,
|
28
|
+
co_temperature: Optional[float] = None,
|
29
|
+
co_top_p: Optional[float] = None,
|
30
|
+
co_max_tokens: Optional[int] = None,
|
31
|
+
max_validations: int = 2,
|
32
|
+
default: None = None,
|
33
|
+
**kwargs: Unpack[GenerateKwargs],
|
34
|
+
) -> T | None: ...
|
35
|
+
|
36
|
+
@overload
|
37
|
+
async def aask_covalidate[T](
|
38
|
+
self,
|
39
|
+
question: str,
|
40
|
+
validator: Callable[[str], T | None],
|
41
|
+
co_model: Optional[str] = None,
|
42
|
+
co_temperature: Optional[float] = None,
|
43
|
+
co_top_p: Optional[float] = None,
|
44
|
+
co_max_tokens: Optional[int] = None,
|
45
|
+
max_validations: int = 2,
|
46
|
+
default: T = ...,
|
47
|
+
**kwargs: Unpack[GenerateKwargs],
|
48
|
+
) -> T: ...
|
49
|
+
|
50
|
+
@overload
|
51
|
+
async def aask_covalidate[T](
|
52
|
+
self,
|
53
|
+
question: List[str],
|
54
|
+
validator: Callable[[str], T | None],
|
55
|
+
co_model: Optional[str] = None,
|
56
|
+
co_temperature: Optional[float] = None,
|
57
|
+
co_top_p: Optional[float] = None,
|
58
|
+
co_max_tokens: Optional[int] = None,
|
59
|
+
max_validations: int = 2,
|
60
|
+
default: None = None,
|
61
|
+
**kwargs: Unpack[GenerateKwargs],
|
62
|
+
) -> List[T | None]: ...
|
63
|
+
|
64
|
+
@overload
|
65
|
+
async def aask_covalidate[T](
|
66
|
+
self,
|
67
|
+
question: List[str],
|
68
|
+
validator: Callable[[str], T | None],
|
69
|
+
co_model: Optional[str] = None,
|
70
|
+
co_temperature: Optional[float] = None,
|
71
|
+
co_top_p: Optional[float] = None,
|
72
|
+
co_max_tokens: Optional[int] = None,
|
73
|
+
max_validations: int = 2,
|
74
|
+
default: T = ...,
|
75
|
+
**kwargs: Unpack[GenerateKwargs],
|
76
|
+
) -> List[T]: ...
|
77
|
+
|
78
|
+
async def aask_covalidate[T](
|
79
|
+
self,
|
80
|
+
question: Union[str, List[str]],
|
81
|
+
validator: Callable[[str], T | None],
|
82
|
+
co_model: Optional[str] = None,
|
83
|
+
co_temperature: Optional[float] = None,
|
84
|
+
co_top_p: Optional[float] = None,
|
85
|
+
co_max_tokens: Optional[int] = None,
|
86
|
+
max_validations: int = 2,
|
87
|
+
default: Optional[T] = None,
|
88
|
+
**kwargs: Unpack[GenerateKwargs],
|
89
|
+
) -> Union[T | None, List[T | None]]:
|
90
|
+
"""Ask the LLM with co-validation to obtain a validated response.
|
91
|
+
|
92
|
+
This method attempts to generate a response that passes validation using two approaches:
|
93
|
+
1. First, it asks the primary LLM using the original question
|
94
|
+
2. If validation fails, it uses a secondary (co-) model with a template to improve the response
|
95
|
+
3. The process repeats up to max_validations times
|
96
|
+
|
97
|
+
Args:
|
98
|
+
question: String question or list of questions to ask
|
99
|
+
validator: Function that validates responses, returns result or None if invalid
|
100
|
+
co_model: Optional model name for the co-validator
|
101
|
+
co_temperature: Optional temperature setting for the co-validator
|
102
|
+
co_top_p: Optional top_p setting for the co-validator
|
103
|
+
co_max_tokens: Optional maximum tokens for the co-validator response
|
104
|
+
max_validations: Maximum number of validation attempts
|
105
|
+
default: Default value to return if validation fails
|
106
|
+
**kwargs: Additional keyword arguments passed to aask method
|
107
|
+
|
108
|
+
Returns:
|
109
|
+
The validated result (T) or default if validation fails.
|
110
|
+
If input is a list of questions, returns a list of results.
|
111
|
+
"""
|
112
|
+
|
113
|
+
async def validate_single_question(q: str) -> Optional[T]:
|
114
|
+
"""Process a single question with validation attempts."""
|
115
|
+
validation_kwargs = kwargs.copy()
|
116
|
+
|
117
|
+
for lap in range(max_validations):
|
118
|
+
try:
|
119
|
+
# First attempt: direct question to primary model
|
120
|
+
response = await self.aask(question=q, **validation_kwargs)
|
121
|
+
if response and (validated := validator(response)):
|
122
|
+
logger.debug(f"Successfully validated the primary response at {lap}th attempt.")
|
123
|
+
return validated
|
124
|
+
|
125
|
+
# Second attempt: use co-model with validation template
|
126
|
+
co_prompt = TEMPLATE_MANAGER.render_template(
|
127
|
+
configs.templates.co_validation_template,
|
128
|
+
{"original_q": q, "original_a": response},
|
129
|
+
)
|
130
|
+
co_response = await self.aask(
|
131
|
+
question=co_prompt,
|
132
|
+
model=co_model,
|
133
|
+
temperature=co_temperature,
|
134
|
+
top_p=co_top_p,
|
135
|
+
max_tokens=co_max_tokens,
|
136
|
+
)
|
137
|
+
|
138
|
+
if co_response and (validated := validator(co_response)):
|
139
|
+
logger.debug(f"Successfully validated the co-response at {lap}th attempt.")
|
140
|
+
return validated
|
141
|
+
|
142
|
+
except Exception as e: # noqa: BLE001
|
143
|
+
logger.error(f"Error during validation: \n{e}")
|
144
|
+
break
|
145
|
+
|
146
|
+
# Disable caching for subsequent attempts
|
147
|
+
if not validation_kwargs.get("no_cache"):
|
148
|
+
validation_kwargs["no_cache"] = True
|
149
|
+
logger.debug("Disabled cache for the next attempt")
|
150
|
+
|
151
|
+
if default is None:
|
152
|
+
logger.error(f"Failed to validate the response after {max_validations} attempts.")
|
153
|
+
return default
|
154
|
+
|
155
|
+
# Handle single question or list of questions
|
156
|
+
if isinstance(question, str):
|
157
|
+
return await validate_single_question(question)
|
158
|
+
|
159
|
+
# Process multiple questions in parallel
|
160
|
+
return await gather(*[validate_single_question(q) for q in question])
|
@@ -84,7 +84,7 @@ class HandleTask(WithBriefing, ToolBoxUsage):
|
|
84
84
|
**self.prepend(cast(Dict[str, Any], kwargs)),
|
85
85
|
)
|
86
86
|
|
87
|
-
async def
|
87
|
+
async def handle_fine_grind(
|
88
88
|
self,
|
89
89
|
task: Task,
|
90
90
|
data: Dict[str, Any],
|
@@ -110,4 +110,4 @@ class HandleTask(WithBriefing, ToolBoxUsage):
|
|
110
110
|
|
111
111
|
async def handle(self, task: Task, data: Dict[str, Any], **kwargs: Unpack[ValidateKwargs]) -> Optional[Tuple]:
|
112
112
|
"""Asynchronously handles a task based on a given task object and parameters."""
|
113
|
-
return await self.
|
113
|
+
return await self.handle_fine_grind(task, data, **kwargs)
|
@@ -48,37 +48,37 @@ class LLMConfig(BaseModel):
|
|
48
48
|
"""
|
49
49
|
|
50
50
|
model_config = ConfigDict(use_attribute_docstrings=True)
|
51
|
-
api_endpoint: HttpUrl = Field(default=HttpUrl("https://api.openai.com"))
|
51
|
+
api_endpoint: Optional[HttpUrl] = Field(default=HttpUrl("https://api.openai.com"))
|
52
52
|
"""OpenAI API Endpoint."""
|
53
53
|
|
54
|
-
api_key: SecretStr = Field(default=SecretStr(""))
|
54
|
+
api_key: Optional[SecretStr] = Field(default=SecretStr("sk-setyourkey"))
|
55
55
|
"""OpenAI API key. Empty by default for security reasons, should be set before use."""
|
56
56
|
|
57
|
-
timeout: PositiveInt = Field(default=300)
|
57
|
+
timeout: Optional[PositiveInt] = Field(default=300)
|
58
58
|
"""The timeout of the LLM model in seconds. Default is 300 seconds as per request."""
|
59
59
|
|
60
|
-
max_retries: PositiveInt = Field(default=3)
|
60
|
+
max_retries: Optional[PositiveInt] = Field(default=3)
|
61
61
|
"""The maximum number of retries. Default is 3 retries."""
|
62
62
|
|
63
|
-
model: str = Field(default="gpt-3.5-turbo")
|
63
|
+
model: Optional[str] = Field(default="gpt-3.5-turbo")
|
64
64
|
"""The LLM model name. Set to 'gpt-3.5-turbo' as per request."""
|
65
65
|
|
66
|
-
temperature: NonNegativeFloat = Field(default=1.0)
|
66
|
+
temperature: Optional[NonNegativeFloat] = Field(default=1.0)
|
67
67
|
"""The temperature of the LLM model. Controls randomness in generation. Set to 1.0 as per request."""
|
68
68
|
|
69
|
-
stop_sign: str | List[str] = Field(
|
69
|
+
stop_sign: Optional[str | List[str]] = Field(default="")
|
70
70
|
"""The stop sign of the LLM model. No default stop sign specified."""
|
71
71
|
|
72
|
-
top_p: NonNegativeFloat = Field(default=0.35)
|
72
|
+
top_p: Optional[NonNegativeFloat] = Field(default=0.35)
|
73
73
|
"""The top p of the LLM model. Controls diversity via nucleus sampling. Set to 0.35 as per request."""
|
74
74
|
|
75
|
-
generation_count: PositiveInt = Field(default=1)
|
75
|
+
generation_count: Optional[PositiveInt] = Field(default=1)
|
76
76
|
"""The number of generations to generate. Default is 1."""
|
77
77
|
|
78
|
-
stream: bool = Field(default=False)
|
78
|
+
stream: Optional[bool] = Field(default=False)
|
79
79
|
"""Whether to stream the LLM model's response. Default is False."""
|
80
80
|
|
81
|
-
max_tokens: PositiveInt = Field(default=8192)
|
81
|
+
max_tokens: Optional[PositiveInt] = Field(default=8192)
|
82
82
|
"""The maximum number of tokens to generate. Set to 8192 as per request."""
|
83
83
|
|
84
84
|
rpm: Optional[PositiveInt] = Field(default=100)
|
@@ -93,7 +93,7 @@ class EmbeddingConfig(BaseModel):
|
|
93
93
|
|
94
94
|
model_config = ConfigDict(use_attribute_docstrings=True)
|
95
95
|
|
96
|
-
model: str = Field(default="text-embedding-ada-002")
|
96
|
+
model: Optional[str] = Field(default="text-embedding-ada-002")
|
97
97
|
"""The embedding model name. """
|
98
98
|
|
99
99
|
dimensions: Optional[PositiveInt] = Field(default=None)
|
@@ -102,10 +102,10 @@ class EmbeddingConfig(BaseModel):
|
|
102
102
|
timeout: Optional[PositiveInt] = Field(default=None)
|
103
103
|
"""The timeout of the embedding model in seconds."""
|
104
104
|
|
105
|
-
max_sequence_length: PositiveInt = Field(default=8192)
|
105
|
+
max_sequence_length: Optional[PositiveInt] = Field(default=8192)
|
106
106
|
"""The maximum sequence length of the embedding model. Default is 8192 as per request."""
|
107
107
|
|
108
|
-
caching: bool = Field(default=False)
|
108
|
+
caching: Optional[bool] = Field(default=False)
|
109
109
|
"""Whether to cache the embedding. Default is False."""
|
110
110
|
|
111
111
|
api_endpoint: Optional[HttpUrl] = None
|
@@ -232,6 +232,9 @@ class TemplateConfig(BaseModel):
|
|
232
232
|
correct_template: str = Field(default="correct")
|
233
233
|
"""The name of the correct template which will be used to correct a string."""
|
234
234
|
|
235
|
+
co_validation_template: str = Field(default="co_validation")
|
236
|
+
"""The name of the co-validation template which will be used to co-validate a string."""
|
237
|
+
|
235
238
|
|
236
239
|
class MagikaConfig(BaseModel):
|
237
240
|
"""Magika configuration class."""
|
@@ -272,7 +275,7 @@ class RagConfig(BaseModel):
|
|
272
275
|
|
273
276
|
model_config = ConfigDict(use_attribute_docstrings=True)
|
274
277
|
|
275
|
-
milvus_uri: HttpUrl = Field(default=HttpUrl("http://localhost:19530"))
|
278
|
+
milvus_uri: Optional[HttpUrl] = Field(default=HttpUrl("http://localhost:19530"))
|
276
279
|
"""The URI of the Milvus server."""
|
277
280
|
milvus_timeout: Optional[PositiveFloat] = Field(default=None)
|
278
281
|
"""The timeout of the Milvus server."""
|