fabricatio 0.2.9.dev4__tar.gz → 0.2.10.dev0__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.9.dev4 → fabricatio-0.2.10.dev0}/PKG-INFO +1 -1
- fabricatio-0.2.10.dev0/examples/write_article/write_article.py +241 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/pyproject.toml +1 -1
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/actions/article.py +9 -105
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/capabilities/check.py +2 -1
- fabricatio-0.2.10.dev0/python/fabricatio/capabilities/rag.py +229 -0
- fabricatio-0.2.10.dev0/python/fabricatio/constants.py +20 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/decorators.py +23 -0
- fabricatio-0.2.10.dev0/python/fabricatio/models/adv_kwargs_types.py +67 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/models/events.py +6 -6
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/models/extra/advanced_judge.py +2 -2
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/models/extra/article_base.py +2 -186
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/models/extra/article_main.py +12 -107
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/models/extra/problem.py +12 -17
- fabricatio-0.2.10.dev0/python/fabricatio/models/extra/rag.py +72 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/models/extra/rule.py +1 -2
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/models/generic.py +16 -9
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/models/kwargs_types.py +1 -38
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/models/task.py +3 -3
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/models/usages.py +73 -5
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/rust.pyi +25 -5
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/src/bib_tools.rs +21 -25
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/src/hash.rs +1 -1
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/src/hbs_helpers.rs +1 -1
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/src/language.rs +1 -2
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/src/templates.rs +1 -0
- fabricatio-0.2.10.dev0/src/word_split.rs +86 -0
- fabricatio-0.2.10.dev0/templates/built-in/check_string.hbs +22 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/extract_criteria_from_reasons.hbs +3 -3
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/extract_reasons_from_examples.hbs +5 -3
- fabricatio-0.2.10.dev0/templates.tar.gz +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/tests/test_models/test_action.py +1 -1
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/tests/test_models/test_problem.py +7 -7
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/tests/test_models/test_task.py +1 -1
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/uv.lock +22 -22
- fabricatio-0.2.9.dev4/examples/write_article/write_article.py +0 -64
- fabricatio-0.2.9.dev4/python/fabricatio/capabilities/rag.py +0 -422
- fabricatio-0.2.9.dev4/python/fabricatio/models/adv_kwargs_types.py +0 -25
- fabricatio-0.2.9.dev4/python/fabricatio/models/utils.py +0 -148
- fabricatio-0.2.9.dev4/src/word_split.rs +0 -27
- fabricatio-0.2.9.dev4/templates/built-in/check_string.hbs +0 -19
- fabricatio-0.2.9.dev4/templates.tar.gz +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/.github/workflows/build-package.yaml +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/.github/workflows/ruff.yaml +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/.github/workflows/tests.yaml +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/.gitignore +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/.python-version +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/Cargo.lock +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/Cargo.toml +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/LICENSE +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/Makefile +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/README.md +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/correct/correct.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/correct/correct_loop.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/extract_and_inject/.gitignore +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/extract_and_inject/article_rag.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/extract_and_inject/ask.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/extract_and_inject/extract_and_inject.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/extract_article/extract.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/llm_usages/llm_usage.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/make_a_rating/rating.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/make_diary/commits.json +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/make_diary/diary.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/minor/hello_fabricatio.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/minor/write_a_poem.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/propose_task/propose.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/reviewer/censor.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/reviewer/review.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/rules/.gitignore +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/rules/draft_ruleset.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/search_bibtex/.gitignore +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/search_bibtex/search.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/simple_chat/chat.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/simple_rag/simple_rag.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/task_handle/handle_task.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/write_article/.gitignore +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/write_article/article_rag.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/write_outline/.gitignore +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/write_outline/write_outline.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/examples/write_outline/write_outline_corrected.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/__init__.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/actions/__init__.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/actions/article_rag.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/actions/output.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/actions/rag.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/actions/rules.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/capabilities/__init__.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/capabilities/advanced_judge.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/capabilities/censor.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/capabilities/correct.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/capabilities/propose.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/capabilities/rating.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/capabilities/review.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/capabilities/task.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/config.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/core.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/fs/__init__.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/fs/curd.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/fs/readers.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/journal.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/models/action.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/models/extra/__init__.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/models/extra/article_essence.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/models/extra/article_outline.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/models/extra/article_proposal.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/models/extra/patches.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/models/role.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/models/tool.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/parser.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/py.typed +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/rust_instances.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/toolboxes/__init__.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/toolboxes/arithmetic.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/toolboxes/fs.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/utils.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/workflows/__init__.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/workflows/articles.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/python/fabricatio/workflows/rag.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/src/lib.rs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/as_prompt.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/binary-exploitation-ctf-solver.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/claude-xml.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/clean-up-code.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/co_validation.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/create_json_obj.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/cryptography-ctf-solver.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/dependencies.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/document-the-code.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/draft_rating_criteria.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/draft_rating_manual.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/draft_rating_weights_klee.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/draft_tool_usage_code.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/find-security-vulnerabilities.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/fix-bugs.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/fix_troubled_obj.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/fix_troubled_string.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/generic_string.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/improve-performance.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/liststr.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/make_choice.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/make_judgment.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/pathstr.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/rate_fine_grind.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/refactor.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/refined_query.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/retrieved_display.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/reverse-engineering-ctf-solver.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/review_string.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/rule_requirement.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/ruleset_requirement_breakdown.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/task_briefing.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/web-ctf-solver.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/write-git-commit.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/write-github-pull-request.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/templates/built-in/write-github-readme.hbs +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/tests/test_config.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/tests/test_models/test_advanced.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/tests/test_models/test_generic.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/tests/test_models/test_role.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/tests/test_models/test_tool.py +0 -0
- {fabricatio-0.2.9.dev4 → fabricatio-0.2.10.dev0}/tests/test_models/test_usages.py +0 -0
@@ -0,0 +1,241 @@
|
|
1
|
+
"""Example of using the library."""
|
2
|
+
|
3
|
+
import asyncio
|
4
|
+
from pathlib import Path
|
5
|
+
from typing import List
|
6
|
+
|
7
|
+
from fabricatio import Event, Role, WorkFlow, logger
|
8
|
+
from fabricatio.actions.article import (
|
9
|
+
FixIntrospectedErrors,
|
10
|
+
GenerateArticle,
|
11
|
+
GenerateArticleProposal,
|
12
|
+
GenerateInitialOutline,
|
13
|
+
)
|
14
|
+
from fabricatio.actions.article_rag import TweakArticleRAG
|
15
|
+
from fabricatio.actions.output import DumpFinalizedOutput, GatherAsList, PersistentAll, RetrieveFromLatest
|
16
|
+
from fabricatio.actions.rules import DraftRuleSet, GatherRuleset
|
17
|
+
from fabricatio.models.action import Action
|
18
|
+
from fabricatio.models.extra.article_main import Article
|
19
|
+
from fabricatio.models.extra.article_outline import ArticleOutline
|
20
|
+
from fabricatio.models.extra.article_proposal import ArticleProposal
|
21
|
+
from fabricatio.models.extra.rule import RuleSet
|
22
|
+
from fabricatio.models.task import Task
|
23
|
+
from fabricatio.utils import ok
|
24
|
+
|
25
|
+
|
26
|
+
class Connect(Action):
|
27
|
+
"""Connect the article with the article_outline and article_proposal."""
|
28
|
+
|
29
|
+
output_key: str = "article"
|
30
|
+
"""Connect the article with the article_outline and article_proposal."""
|
31
|
+
|
32
|
+
async def _execute(
|
33
|
+
self,
|
34
|
+
article_briefing: str,
|
35
|
+
article_proposal: ArticleProposal,
|
36
|
+
article_outline: ArticleOutline,
|
37
|
+
article: Article,
|
38
|
+
**cxt,
|
39
|
+
) -> Article:
|
40
|
+
"""Connect the article with the article_outline and article_proposal."""
|
41
|
+
return article.update_ref(article_outline.update_ref(article_proposal.update_ref(article_briefing)))
|
42
|
+
|
43
|
+
|
44
|
+
async def main(article: bool, rule: bool = False, fintune: bool = False) -> None:
|
45
|
+
"""Main function."""
|
46
|
+
Role(
|
47
|
+
name="Undergraduate Researcher",
|
48
|
+
description="Write an outline for an article in typst format.",
|
49
|
+
llm_model="openai/deepseek-v3-250324",
|
50
|
+
llm_temperature=1.3,
|
51
|
+
llm_top_p=1.0,
|
52
|
+
llm_max_tokens=8191,
|
53
|
+
llm_rpm=1000,
|
54
|
+
llm_tpm=5000000,
|
55
|
+
registry={
|
56
|
+
Event.quick_instantiate(ns := "article"): WorkFlow(
|
57
|
+
name="Generate Article Outline",
|
58
|
+
description="Generate an outline for an article. dump the outline to the given path. in typst format.",
|
59
|
+
steps=(
|
60
|
+
RetrieveFromLatest(
|
61
|
+
retrieve_cls=RuleSet,
|
62
|
+
load_path="persistent_ruleset/outline_ruleset",
|
63
|
+
output_key="outline_ruleset",
|
64
|
+
),
|
65
|
+
RetrieveFromLatest(
|
66
|
+
retrieve_cls=RuleSet,
|
67
|
+
load_path="persistent_ruleset/dep_ref_ruleset",
|
68
|
+
output_key="dep_ref_ruleset",
|
69
|
+
),
|
70
|
+
RetrieveFromLatest(
|
71
|
+
retrieve_cls=RuleSet,
|
72
|
+
load_path="persistent_ruleset/rev_dep_ref_ruleset",
|
73
|
+
output_key="rev_dep_ref_ruleset",
|
74
|
+
),
|
75
|
+
RetrieveFromLatest(
|
76
|
+
retrieve_cls=RuleSet, load_path="persistent_ruleset/cite_ruleset", output_key="cite_ruleset"
|
77
|
+
),
|
78
|
+
RetrieveFromLatest(
|
79
|
+
retrieve_cls=RuleSet, load_path="persistent_ruleset/para_ruleset", output_key="para_ruleset"
|
80
|
+
),
|
81
|
+
RetrieveFromLatest(
|
82
|
+
retrieve_cls=RuleSet, load_path="persistent_ruleset/ref_ruleset", output_key="ref_ruleset"
|
83
|
+
),
|
84
|
+
RetrieveFromLatest(
|
85
|
+
retrieve_cls=RuleSet, load_path="persistent_ruleset/lang_ruleset", output_key="lang_ruleset"
|
86
|
+
),
|
87
|
+
GenerateArticleProposal(llm_temperature=1.3),
|
88
|
+
GenerateInitialOutline(
|
89
|
+
output_key="article_outline",
|
90
|
+
),
|
91
|
+
GatherRuleset(output_key="intro_fix_ruleset", to_gather=["para_ruleset"]),
|
92
|
+
FixIntrospectedErrors(
|
93
|
+
output_key="article_outline",
|
94
|
+
),
|
95
|
+
PersistentAll,
|
96
|
+
GatherRuleset(output_key="ref_fix_ruleset", to_gather=["ref_ruleset"]),
|
97
|
+
GatherRuleset(output_key="ref_twk_ruleset", to_gather=["dep_ref_ruleset", "ref_ruleset"]),
|
98
|
+
GatherRuleset(output_key="article_gen_ruleset", to_gather=["para_ruleset"]),
|
99
|
+
GenerateArticle(
|
100
|
+
output_key="article",
|
101
|
+
),
|
102
|
+
PersistentAll,
|
103
|
+
GatherRuleset(
|
104
|
+
output_key="twk_rag_ruleset", to_gather=["para_ruleset", "cite_ruleset", "lang_ruleset"]
|
105
|
+
),
|
106
|
+
TweakArticleRAG(output_key="to_dump"),
|
107
|
+
DumpFinalizedOutput(output_key="task_output"),
|
108
|
+
PersistentAll,
|
109
|
+
),
|
110
|
+
).update_init_context(
|
111
|
+
article_briefing=Path("./article_briefing.txt").read_text(),
|
112
|
+
dump_path="out.typ",
|
113
|
+
persist_dir="persistent",
|
114
|
+
collection_name="article_essence_0324",
|
115
|
+
),
|
116
|
+
Event.quick_instantiate(finetune := "article_finetune"): WorkFlow(
|
117
|
+
name="Generate Article Outline",
|
118
|
+
description="Generate an outline for an article. dump the outline to the given path. in typst format.",
|
119
|
+
steps=(
|
120
|
+
RetrieveFromLatest(
|
121
|
+
retrieve_cls=RuleSet,
|
122
|
+
load_path="persistent_ruleset/outline_ruleset",
|
123
|
+
output_key="outline_ruleset",
|
124
|
+
),
|
125
|
+
RetrieveFromLatest(
|
126
|
+
retrieve_cls=RuleSet,
|
127
|
+
load_path="persistent_ruleset/dep_ref_ruleset",
|
128
|
+
output_key="dep_ref_ruleset",
|
129
|
+
),
|
130
|
+
RetrieveFromLatest(
|
131
|
+
retrieve_cls=RuleSet,
|
132
|
+
load_path="persistent_ruleset/rev_dep_ref_ruleset",
|
133
|
+
output_key="rev_dep_ref_ruleset",
|
134
|
+
),
|
135
|
+
RetrieveFromLatest(
|
136
|
+
retrieve_cls=RuleSet, load_path="persistent_ruleset/cite_ruleset", output_key="cite_ruleset"
|
137
|
+
),
|
138
|
+
RetrieveFromLatest(
|
139
|
+
retrieve_cls=RuleSet, load_path="persistent_ruleset/para_ruleset", output_key="para_ruleset"
|
140
|
+
),
|
141
|
+
RetrieveFromLatest(
|
142
|
+
retrieve_cls=RuleSet, load_path="persistent_ruleset/ref_ruleset", output_key="ref_ruleset"
|
143
|
+
),
|
144
|
+
RetrieveFromLatest(
|
145
|
+
retrieve_cls=ArticleProposal,
|
146
|
+
load_path="persistent/article_proposal",
|
147
|
+
output_key="article_proposal",
|
148
|
+
),
|
149
|
+
RetrieveFromLatest(
|
150
|
+
retrieve_cls=ArticleOutline,
|
151
|
+
load_path="persistent/article_outline",
|
152
|
+
output_key="article_outline",
|
153
|
+
),
|
154
|
+
RetrieveFromLatest(retrieve_cls=Article, load_path="persistent/article", output_key="article"),
|
155
|
+
Connect,
|
156
|
+
GatherRuleset(output_key="intro_fix_ruleset", to_gather=["para_ruleset"]),
|
157
|
+
GatherRuleset(output_key="ref_fix_ruleset", to_gather=["ref_ruleset"]),
|
158
|
+
GatherRuleset(output_key="article_gen_ruleset", to_gather=["para_ruleset"]),
|
159
|
+
GatherRuleset(output_key="twk_rag_ruleset", to_gather=["para_ruleset", "cite_ruleset"]),
|
160
|
+
TweakArticleRAG(
|
161
|
+
output_key="to_dump",
|
162
|
+
),
|
163
|
+
DumpFinalizedOutput(output_key="task_output"),
|
164
|
+
PersistentAll,
|
165
|
+
),
|
166
|
+
).update_init_context(
|
167
|
+
article_briefing=Path("./article_briefing.txt").read_text(),
|
168
|
+
dump_path="out_fix.typ",
|
169
|
+
persist_dir="persistent_fix",
|
170
|
+
collection_name="article_essence_0324",
|
171
|
+
),
|
172
|
+
Event.quick_instantiate(rule_ns := "rule"): WorkFlow(
|
173
|
+
name="Generate Draft Rule Set",
|
174
|
+
description="Generate a draft rule set for the article.",
|
175
|
+
llm_model="openai/deepseek-v3-250324",
|
176
|
+
llm_stream=False,
|
177
|
+
steps=(
|
178
|
+
# 精简后的para_ruleset规则
|
179
|
+
DraftRuleSet(
|
180
|
+
ruleset_requirement="如果`paragraphs`字段为空列表,那么你就需要按照`expected_word_count`来为章节补充内容",
|
181
|
+
output_key="para_ruleset",
|
182
|
+
rule_count=1,
|
183
|
+
),
|
184
|
+
# 精简后的cite_ruleset规则
|
185
|
+
DraftRuleSet(
|
186
|
+
ruleset_requirement="1. 参考文献引用格式:(作者等, 年份)#cite(<bibtex_key>)\n"
|
187
|
+
"2. #cite()必须用尖括号包裹单个BibTeX键,多引用需重复使用",
|
188
|
+
output_key="cite_ruleset",
|
189
|
+
rule_count=1,
|
190
|
+
),
|
191
|
+
# 新增中文检测规则集
|
192
|
+
DraftRuleSet(
|
193
|
+
ruleset_requirement="1. 所有标题和正文内容必须使用中文,如果不为中文你需要翻译过来\n"
|
194
|
+
"2. 术语和专业词汇需使用中文表述,英文缩写第一次出现的时候需要在其后面‘()’来辅助说明",
|
195
|
+
output_key="lang_ruleset",
|
196
|
+
rule_count=1,
|
197
|
+
),
|
198
|
+
# 其他规则集保持原有结构但简化内容
|
199
|
+
DraftRuleSet(
|
200
|
+
ruleset_requirement="章节的`depend_on`字段的`ArticleRef`只能引用当前章节之前的元素。\n",
|
201
|
+
output_key="dep_ref_ruleset",
|
202
|
+
rule_count=1,
|
203
|
+
),
|
204
|
+
DraftRuleSet(
|
205
|
+
ruleset_requirement="章节的`support_to`字段的`ArticleRef`只能引用当前章节之后的元素。\n",
|
206
|
+
output_key="rev_dep_ref_ruleset",
|
207
|
+
rule_count=1,
|
208
|
+
),
|
209
|
+
DraftRuleSet(
|
210
|
+
ruleset_requirement="ArticleRef必须指向已定义元素",
|
211
|
+
output_key="ref_ruleset",
|
212
|
+
rule_count=1,
|
213
|
+
),
|
214
|
+
DraftRuleSet(
|
215
|
+
ruleset_requirement="标题使用学术术语",
|
216
|
+
output_key="outline_ruleset",
|
217
|
+
rule_count=1,
|
218
|
+
),
|
219
|
+
GatherAsList(gather_suffix="ruleset").to_task_output(),
|
220
|
+
PersistentAll(persist_dir="persistent_ruleset"),
|
221
|
+
),
|
222
|
+
),
|
223
|
+
},
|
224
|
+
)
|
225
|
+
|
226
|
+
if rule:
|
227
|
+
draft_rule_task: Task[List[RuleSet]] = Task(name="draft a rule set")
|
228
|
+
rule_set = ok(await draft_rule_task.delegate(rule_ns), "Failed to generate ruleset")
|
229
|
+
logger.success(f"Ruleset:\n{len(rule_set)}")
|
230
|
+
if article:
|
231
|
+
proposed_task = Task(name="write an article")
|
232
|
+
path = ok(await proposed_task.delegate(ns), "Failed to generate ruleset")
|
233
|
+
logger.success(f"The outline is saved in:\n{path}")
|
234
|
+
if fintune:
|
235
|
+
proposed_task = Task(name="finetune an article")
|
236
|
+
path = ok(await proposed_task.delegate(finetune), "Failed to generate ruleset")
|
237
|
+
logger.success(f"The outline is saved in:\n{path}")
|
238
|
+
|
239
|
+
|
240
|
+
if __name__ == "__main__":
|
241
|
+
asyncio.run(main(True, False, False))
|
@@ -11,7 +11,6 @@ from fabricatio.capabilities.propose import Propose
|
|
11
11
|
from fabricatio.fs import safe_text_read
|
12
12
|
from fabricatio.journal import logger
|
13
13
|
from fabricatio.models.action import Action
|
14
|
-
from fabricatio.models.extra.article_base import SubSectionBase
|
15
14
|
from fabricatio.models.extra.article_essence import ArticleEssence
|
16
15
|
from fabricatio.models.extra.article_main import Article
|
17
16
|
from fabricatio.models.extra.article_outline import ArticleOutline
|
@@ -142,11 +141,17 @@ class GenerateInitialOutline(Action, Propose):
|
|
142
141
|
article_proposal: ArticleProposal,
|
143
142
|
**_,
|
144
143
|
) -> Optional[ArticleOutline]:
|
144
|
+
raw_outline = await self.aask(
|
145
|
+
f"{(article_proposal.as_prompt())}\n\nNote that you should use `{article_proposal.language}` to write the `ArticleOutline`\n"
|
146
|
+
f"Design each chapter of a proper and academic and ready for release manner.\n"
|
147
|
+
f"You Must make sure every chapter have sections, and every section have subsections.\n"
|
148
|
+
f"Make the chapter and sections and subsections bing divided into a specific enough article component.",
|
149
|
+
)
|
150
|
+
|
145
151
|
return ok(
|
146
152
|
await self.propose(
|
147
153
|
ArticleOutline,
|
148
|
-
f"{
|
149
|
-
f"You Must make sure every chapter have sections, and every section have subsections.",
|
154
|
+
f"{raw_outline}\n\n\n\noutline provided above is the outline i need to extract to a JSON,",
|
150
155
|
),
|
151
156
|
"Could not generate the initial outline.",
|
152
157
|
).update_ref(article_proposal)
|
@@ -178,7 +183,7 @@ class FixIntrospectedErrors(Action, Censor):
|
|
178
183
|
await self.censor_obj(
|
179
184
|
article_outline,
|
180
185
|
ruleset=ok(intro_fix_ruleset or self.ruleset, "No ruleset provided"),
|
181
|
-
reference=f"{article_outline.
|
186
|
+
reference=f"{article_outline.display()}\n # Fatal Error of the Original Article Outline\n{pack}",
|
182
187
|
),
|
183
188
|
"Could not correct the component.",
|
184
189
|
).update_ref(origin)
|
@@ -191,107 +196,6 @@ class FixIntrospectedErrors(Action, Censor):
|
|
191
196
|
return article_outline
|
192
197
|
|
193
198
|
|
194
|
-
class FixIllegalReferences(Action, Censor):
|
195
|
-
"""Fix illegal references in the article outline."""
|
196
|
-
|
197
|
-
output_key: str = "illegal_references_fixed_outline"
|
198
|
-
"""The key of the output data."""
|
199
|
-
|
200
|
-
ruleset: Optional[RuleSet] = None
|
201
|
-
"""Ruleset to use to fix the illegal references."""
|
202
|
-
max_error_count: Optional[int] = None
|
203
|
-
"""The maximum number of errors to fix."""
|
204
|
-
|
205
|
-
async def _execute(
|
206
|
-
self,
|
207
|
-
article_outline: ArticleOutline,
|
208
|
-
ref_fix_ruleset: Optional[RuleSet] = None,
|
209
|
-
**_,
|
210
|
-
) -> Optional[ArticleOutline]:
|
211
|
-
counter = 0
|
212
|
-
while pack := article_outline.find_illegal_ref(gather_identical=True):
|
213
|
-
logger.info(f"Found {counter}th illegal references")
|
214
|
-
ref_seq, err = ok(pack)
|
215
|
-
logger.warning(f"Found illegal referring error: {err}")
|
216
|
-
new = ok(
|
217
|
-
await self.censor_obj(
|
218
|
-
ref_seq[0],
|
219
|
-
ruleset=ok(ref_fix_ruleset or self.ruleset, "No ruleset provided"),
|
220
|
-
reference=f"{article_outline.as_prompt()}\n# Some Basic errors found that need to be fixed\n{err}",
|
221
|
-
),
|
222
|
-
"Could not correct the component",
|
223
|
-
)
|
224
|
-
for r in ref_seq:
|
225
|
-
r.update_from(new)
|
226
|
-
if self.max_error_count and counter > self.max_error_count:
|
227
|
-
logger.warning("Max error count reached, stopping.")
|
228
|
-
break
|
229
|
-
counter += 1
|
230
|
-
|
231
|
-
return article_outline
|
232
|
-
|
233
|
-
|
234
|
-
class TweakOutlineForwardRef(Action, Censor):
|
235
|
-
"""Tweak the forward references in the article outline.
|
236
|
-
|
237
|
-
Ensures that the conclusions of the current chapter effectively support the analysis of subsequent chapters.
|
238
|
-
"""
|
239
|
-
|
240
|
-
output_key: str = "article_outline_fw_ref_checked"
|
241
|
-
ruleset: Optional[RuleSet] = None
|
242
|
-
"""Ruleset to use to fix the illegal references."""
|
243
|
-
|
244
|
-
async def _execute(
|
245
|
-
self, article_outline: ArticleOutline, ref_twk_ruleset: Optional[RuleSet] = None, **cxt
|
246
|
-
) -> ArticleOutline:
|
247
|
-
return await self._inner(
|
248
|
-
article_outline,
|
249
|
-
ruleset=ok(ref_twk_ruleset or self.ruleset, "No ruleset provided"),
|
250
|
-
field_name="support_to",
|
251
|
-
)
|
252
|
-
|
253
|
-
async def _inner(self, article_outline: ArticleOutline, ruleset: RuleSet, field_name: str) -> ArticleOutline:
|
254
|
-
await gather(
|
255
|
-
*[self._loop(a[-1], article_outline, field_name, ruleset) for a in article_outline.iter_subsections()],
|
256
|
-
)
|
257
|
-
|
258
|
-
return article_outline
|
259
|
-
|
260
|
-
async def _loop(
|
261
|
-
self, a: SubSectionBase, article_outline: ArticleOutline, field_name: str, ruleset: RuleSet
|
262
|
-
) -> None:
|
263
|
-
if judge := await self.evidently_judge(
|
264
|
-
f"{article_outline.as_prompt()}\n\n{a.display()}\n"
|
265
|
-
f"Does the `{a.__class__.__name__}`'s `{field_name}` field need to be extended or tweaked?"
|
266
|
-
):
|
267
|
-
await self.censor_obj_inplace(
|
268
|
-
a,
|
269
|
-
ruleset=ruleset,
|
270
|
-
reference=f"{article_outline.as_prompt()}\n"
|
271
|
-
f"The Article component titled `{a.title}` whose `{field_name}` field needs to be extended or tweaked.\n"
|
272
|
-
f"# Judgement\n{judge.display()}",
|
273
|
-
)
|
274
|
-
|
275
|
-
|
276
|
-
class TweakOutlineBackwardRef(TweakOutlineForwardRef):
|
277
|
-
"""Tweak the backward references in the article outline.
|
278
|
-
|
279
|
-
Ensures that the prerequisites of the current chapter are correctly referenced in the `depend_on` field.
|
280
|
-
"""
|
281
|
-
|
282
|
-
output_key: str = "article_outline_bw_ref_checked"
|
283
|
-
ruleset: Optional[RuleSet] = None
|
284
|
-
|
285
|
-
async def _execute(
|
286
|
-
self, article_outline: ArticleOutline, ref_twk_ruleset: Optional[RuleSet] = None, **cxt
|
287
|
-
) -> ArticleOutline:
|
288
|
-
return await self._inner(
|
289
|
-
article_outline,
|
290
|
-
ruleset=ok(ref_twk_ruleset or self.ruleset, "No ruleset provided"),
|
291
|
-
field_name="depend_on",
|
292
|
-
)
|
293
|
-
|
294
|
-
|
295
199
|
class GenerateArticle(Action, Censor):
|
296
200
|
"""Generate the article based on the outline."""
|
297
201
|
|
@@ -104,7 +104,8 @@ class Check(AdvancedJudge, Propose):
|
|
104
104
|
- Proposes Improvement only when violation is confirmed
|
105
105
|
"""
|
106
106
|
if judge := await self.evidently_judge(
|
107
|
-
f"# Content to exam\n{input_text}\n\n# Rule Must to follow\n{rule.display()}\nDoes `Content to exam` provided above violate the `
|
107
|
+
f"# Content to exam\n{input_text}\n\n# Rule Must to follow\n{rule.display()}\nDoes `Content to exam` provided above violate the `{rule.name}` provided above?"
|
108
|
+
f"should I take some measure to fix that violation? true for I do need, false for I don't need.",
|
108
109
|
**override_kwargs(kwargs, default=None),
|
109
110
|
):
|
110
111
|
logger.info(f"Rule `{rule.name}` violated: \n{judge.display()}")
|
@@ -0,0 +1,229 @@
|
|
1
|
+
"""A module for the RAG (Retrieval Augmented Generation) model."""
|
2
|
+
|
3
|
+
try:
|
4
|
+
from pymilvus import MilvusClient
|
5
|
+
except ImportError as e:
|
6
|
+
raise RuntimeError(
|
7
|
+
"pymilvus is not installed. Have you installed `fabricatio[rag]` instead of `fabricatio`?"
|
8
|
+
) from e
|
9
|
+
from functools import lru_cache
|
10
|
+
from operator import itemgetter
|
11
|
+
from typing import List, Optional, Self, Type, Unpack
|
12
|
+
|
13
|
+
from more_itertools.recipes import flatten, unique
|
14
|
+
from pydantic import Field, PrivateAttr
|
15
|
+
|
16
|
+
from fabricatio.config import configs
|
17
|
+
from fabricatio.journal import logger
|
18
|
+
from fabricatio.models.adv_kwargs_types import CollectionConfigKwargs, FetchKwargs
|
19
|
+
from fabricatio.models.extra.rag import MilvusDataBase
|
20
|
+
from fabricatio.models.kwargs_types import ChooseKwargs
|
21
|
+
from fabricatio.models.usages import EmbeddingUsage
|
22
|
+
from fabricatio.rust_instances import TEMPLATE_MANAGER
|
23
|
+
from fabricatio.utils import ok
|
24
|
+
|
25
|
+
|
26
|
+
@lru_cache(maxsize=None)
|
27
|
+
def create_client(uri: str, token: str = "", timeout: Optional[float] = None) -> MilvusClient:
|
28
|
+
"""Create a Milvus client."""
|
29
|
+
return MilvusClient(
|
30
|
+
uri=uri,
|
31
|
+
token=token,
|
32
|
+
timeout=timeout,
|
33
|
+
)
|
34
|
+
|
35
|
+
|
36
|
+
class RAG(EmbeddingUsage):
|
37
|
+
"""A class representing the RAG (Retrieval Augmented Generation) model."""
|
38
|
+
|
39
|
+
target_collection: Optional[str] = Field(default=None)
|
40
|
+
"""The name of the collection being viewed."""
|
41
|
+
|
42
|
+
_client: Optional[MilvusClient] = PrivateAttr(None)
|
43
|
+
"""The Milvus client used for the RAG model."""
|
44
|
+
|
45
|
+
@property
|
46
|
+
def client(self) -> MilvusClient:
|
47
|
+
"""Return the Milvus client."""
|
48
|
+
if self._client is None:
|
49
|
+
raise RuntimeError("Client is not initialized. Have you called `self.init_client()`?")
|
50
|
+
return self._client
|
51
|
+
|
52
|
+
def init_client(
|
53
|
+
self,
|
54
|
+
milvus_uri: Optional[str] = None,
|
55
|
+
milvus_token: Optional[str] = None,
|
56
|
+
milvus_timeout: Optional[float] = None,
|
57
|
+
) -> Self:
|
58
|
+
"""Initialize the Milvus client."""
|
59
|
+
self._client = create_client(
|
60
|
+
uri=milvus_uri or ok(self.milvus_uri or configs.rag.milvus_uri).unicode_string(),
|
61
|
+
token=milvus_token
|
62
|
+
or (token.get_secret_value() if (token := (self.milvus_token or configs.rag.milvus_token)) else ""),
|
63
|
+
timeout=milvus_timeout or self.milvus_timeout or configs.rag.milvus_timeout,
|
64
|
+
)
|
65
|
+
return self
|
66
|
+
|
67
|
+
def check_client(self, init: bool = True) -> Self:
|
68
|
+
"""Check if the client is initialized, and if not, initialize it."""
|
69
|
+
if self._client is None and init:
|
70
|
+
return self.init_client()
|
71
|
+
if self._client is None and not init:
|
72
|
+
raise RuntimeError("Client is not initialized. Have you called `self.init_client()`?")
|
73
|
+
return self
|
74
|
+
|
75
|
+
def view(
|
76
|
+
self, collection_name: Optional[str], create: bool = False, **kwargs: Unpack[CollectionConfigKwargs]
|
77
|
+
) -> Self:
|
78
|
+
"""View the specified collection.
|
79
|
+
|
80
|
+
Args:
|
81
|
+
collection_name (str): The name of the collection.
|
82
|
+
create (bool): Whether to create the collection if it does not exist.
|
83
|
+
**kwargs (Unpack[CollectionConfigKwargs]): Additional keyword arguments for collection configuration.
|
84
|
+
"""
|
85
|
+
if create and collection_name and not self.check_client().client.has_collection(collection_name):
|
86
|
+
kwargs["dimension"] = ok(
|
87
|
+
kwargs.get("dimension")
|
88
|
+
or self.milvus_dimensions
|
89
|
+
or configs.rag.milvus_dimensions
|
90
|
+
or self.embedding_dimensions
|
91
|
+
or configs.embedding.dimensions,
|
92
|
+
"`dimension` is not set at any level.",
|
93
|
+
)
|
94
|
+
self.client.create_collection(collection_name, auto_id=True, **kwargs)
|
95
|
+
logger.info(f"Creating collection {collection_name}")
|
96
|
+
|
97
|
+
self.target_collection = collection_name
|
98
|
+
return self
|
99
|
+
|
100
|
+
def quit_viewing(self) -> Self:
|
101
|
+
"""Quit the current view.
|
102
|
+
|
103
|
+
Returns:
|
104
|
+
Self: The current instance, allowing for method chaining.
|
105
|
+
"""
|
106
|
+
return self.view(None)
|
107
|
+
|
108
|
+
@property
|
109
|
+
def safe_target_collection(self) -> str:
|
110
|
+
"""Get the name of the collection being viewed, raise an error if not viewing any collection.
|
111
|
+
|
112
|
+
Returns:
|
113
|
+
str: The name of the collection being viewed.
|
114
|
+
"""
|
115
|
+
return ok(self.target_collection, "No collection is being viewed. Have you called `self.view()`?")
|
116
|
+
|
117
|
+
async def add_document[D: MilvusDataBase](
|
118
|
+
self, data: List[D] | D, collection_name: Optional[str] = None, flush: bool = False
|
119
|
+
) -> Self:
|
120
|
+
"""Adds a document to the specified collection.
|
121
|
+
|
122
|
+
Args:
|
123
|
+
data (Union[Dict[str, Any], MilvusDataBase] | List[Union[Dict[str, Any], MilvusDataBase]]): The data to be added to the collection.
|
124
|
+
collection_name (Optional[str]): The name of the collection. If not provided, the currently viewed collection is used.
|
125
|
+
flush (bool): Whether to flush the collection after insertion.
|
126
|
+
|
127
|
+
Returns:
|
128
|
+
Self: The current instance, allowing for method chaining.
|
129
|
+
"""
|
130
|
+
if isinstance(data, MilvusDataBase):
|
131
|
+
data = [data]
|
132
|
+
|
133
|
+
data_vec = await self.vectorize([d.to_vectorize for d in data])
|
134
|
+
prepared_data = [d.prepare_insertion(vec) for d, vec in zip(data, data_vec, strict=True)]
|
135
|
+
|
136
|
+
c_name = collection_name or self.safe_target_collection
|
137
|
+
self.check_client().client.insert(c_name, prepared_data)
|
138
|
+
|
139
|
+
if flush:
|
140
|
+
logger.debug(f"Flushing collection {c_name}")
|
141
|
+
self.client.flush(c_name)
|
142
|
+
return self
|
143
|
+
|
144
|
+
async def afetch_document[D: MilvusDataBase](
|
145
|
+
self,
|
146
|
+
vecs: List[List[float]],
|
147
|
+
document_model: Type[D],
|
148
|
+
collection_name: Optional[str] = None,
|
149
|
+
similarity_threshold: float = 0.37,
|
150
|
+
result_per_query: int = 10,
|
151
|
+
) -> List[D]:
|
152
|
+
"""Asynchronously fetches documents from a Milvus database based on input vectors.
|
153
|
+
|
154
|
+
Args:
|
155
|
+
vecs (List[List[float]]): A list of vectors to search for in the database.
|
156
|
+
document_model (Type[D]): The model class used to convert fetched data into document objects.
|
157
|
+
collection_name (Optional[str]): The name of the collection to search within.
|
158
|
+
If None, the currently viewed collection is used.
|
159
|
+
similarity_threshold (float): The similarity threshold for vector search. Defaults to 0.37.
|
160
|
+
result_per_query (int): The maximum number of results to return per query. Defaults to 10.
|
161
|
+
|
162
|
+
Returns:
|
163
|
+
List[D]: A list of document objects created from the fetched data.
|
164
|
+
"""
|
165
|
+
# Step 1: Search for vectors
|
166
|
+
search_results = self.check_client().client.search(
|
167
|
+
collection_name or self.safe_target_collection,
|
168
|
+
vecs,
|
169
|
+
search_params={"radius": similarity_threshold},
|
170
|
+
output_fields=list(document_model.model_fields),
|
171
|
+
limit=result_per_query,
|
172
|
+
)
|
173
|
+
|
174
|
+
# Step 2: Flatten the search results
|
175
|
+
flattened_results = flatten(search_results)
|
176
|
+
unique_results = unique(flattened_results, key=itemgetter("id"))
|
177
|
+
# Step 3: Sort by distance (descending)
|
178
|
+
sorted_results = sorted(unique_results, key=itemgetter("distance"), reverse=True)
|
179
|
+
|
180
|
+
logger.debug(
|
181
|
+
f"Fetched {len(sorted_results)} document,searched similarities: {[t['distance'] for t in sorted_results]}"
|
182
|
+
)
|
183
|
+
# Step 4: Extract the entities
|
184
|
+
resp = [result["entity"] for result in sorted_results]
|
185
|
+
|
186
|
+
return document_model.from_sequence(resp)
|
187
|
+
|
188
|
+
async def aretrieve[D: MilvusDataBase](
|
189
|
+
self,
|
190
|
+
query: List[str] | str,
|
191
|
+
final_limit: int = 20,
|
192
|
+
**kwargs: Unpack[FetchKwargs[D]],
|
193
|
+
) -> List[D]:
|
194
|
+
"""Retrieve data from the collection.
|
195
|
+
|
196
|
+
Args:
|
197
|
+
query (List[str] | str): The query to be used for retrieval.
|
198
|
+
final_limit (int): The final limit on the number of results to return.
|
199
|
+
**kwargs (Unpack[FetchKwargs]): Additional keyword arguments for retrieval.
|
200
|
+
|
201
|
+
Returns:
|
202
|
+
List[D]: A list of document objects created from the retrieved data.
|
203
|
+
"""
|
204
|
+
if isinstance(query, str):
|
205
|
+
query = [query]
|
206
|
+
return (
|
207
|
+
await self.afetch_document(
|
208
|
+
vecs=(await self.vectorize(query)),
|
209
|
+
**kwargs,
|
210
|
+
)
|
211
|
+
)[:final_limit]
|
212
|
+
|
213
|
+
async def arefined_query(self, question: List[str] | str, **kwargs: Unpack[ChooseKwargs]) -> Optional[List[str]]:
|
214
|
+
"""Refines the given question using a template.
|
215
|
+
|
216
|
+
Args:
|
217
|
+
question (List[str] | str): The question to be refined.
|
218
|
+
**kwargs (Unpack[ChooseKwargs]): Additional keyword arguments for the refinement process.
|
219
|
+
|
220
|
+
Returns:
|
221
|
+
List[str]: A list of refined questions.
|
222
|
+
"""
|
223
|
+
return await self.alist_str(
|
224
|
+
TEMPLATE_MANAGER.render_template(
|
225
|
+
configs.templates.refined_query_template,
|
226
|
+
{"question": [question] if isinstance(question, str) else question},
|
227
|
+
),
|
228
|
+
**kwargs,
|
229
|
+
)
|
@@ -0,0 +1,20 @@
|
|
1
|
+
"""A module containing constants used throughout the library."""
|
2
|
+
from enum import StrEnum
|
3
|
+
|
4
|
+
|
5
|
+
class TaskStatus(StrEnum):
|
6
|
+
"""An enumeration representing the status of a task.
|
7
|
+
|
8
|
+
Attributes:
|
9
|
+
Pending: The task is pending.
|
10
|
+
Running: The task is currently running.
|
11
|
+
Finished: The task has been successfully completed.
|
12
|
+
Failed: The task has failed.
|
13
|
+
Cancelled: The task has been cancelled.
|
14
|
+
"""
|
15
|
+
|
16
|
+
Pending = "pending"
|
17
|
+
Running = "running"
|
18
|
+
Finished = "finished"
|
19
|
+
Failed = "failed"
|
20
|
+
Cancelled = "cancelled"
|