rusticai-showcase 0.0.1__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.
- rusticai_showcase-0.0.1/PKG-INFO +41 -0
- rusticai_showcase-0.0.1/README.md +16 -0
- rusticai_showcase-0.0.1/pyproject.toml +74 -0
- rusticai_showcase-0.0.1/src/rustic_ai/showcase/__init__.py +0 -0
- rusticai_showcase-0.0.1/src/rustic_ai/showcase/laira/__init__.py +0 -0
- rusticai_showcase-0.0.1/src/rustic_ai/showcase/laira/research_manager.py +166 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: rusticai-showcase
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Module that showcases Rustic AI capabilities
|
|
5
|
+
Home-page: https://www.rustic.ai/
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Author: Dragonscale Industries Inc.
|
|
8
|
+
Author-email: dev@dragonscale.ai
|
|
9
|
+
Requires-Python: >=3.12,<4.0
|
|
10
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Provides-Extra: test
|
|
15
|
+
Requires-Dist: rusticai-chroma (>=0.0.3,<0.0.4)
|
|
16
|
+
Requires-Dist: rusticai-core (>=0.0.6,<0.0.7)
|
|
17
|
+
Requires-Dist: rusticai-langchain (>=0.0.2,<0.0.3)
|
|
18
|
+
Requires-Dist: rusticai-litellm (>=0.0.4,<0.0.5)
|
|
19
|
+
Requires-Dist: rusticai-playwright (>=0.0.2,<0.0.3)
|
|
20
|
+
Requires-Dist: rusticai-serpapi (>=0.0.2,<0.0.3)
|
|
21
|
+
Project-URL: Repository, https://github.com/rustic-ai/python-framework
|
|
22
|
+
Project-URL: Rustic AI Core, https://pypi.org/project/rusticai-core/
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# Rustic AI Showcase
|
|
26
|
+
|
|
27
|
+
Module that showcases Rustic AI capabilities
|
|
28
|
+
|
|
29
|
+
## Installing
|
|
30
|
+
|
|
31
|
+
```shell
|
|
32
|
+
pip install rusticai-showcase
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Building from Source
|
|
36
|
+
|
|
37
|
+
```shell
|
|
38
|
+
poetry install --with dev
|
|
39
|
+
poetry build
|
|
40
|
+
```
|
|
41
|
+
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["poetry-core"]
|
|
3
|
+
build-backend = "poetry.core.masonry.api"
|
|
4
|
+
|
|
5
|
+
[tool.poetry]
|
|
6
|
+
name = "rusticai-showcase"
|
|
7
|
+
version = "0.0.1"
|
|
8
|
+
description = "Module that showcases Rustic AI capabilities"
|
|
9
|
+
authors = ["Dragonscale Industries Inc. <dev@dragonscale.ai>"]
|
|
10
|
+
license = "Apache-2.0"
|
|
11
|
+
readme = "README.md"
|
|
12
|
+
homepage = "https://www.rustic.ai/"
|
|
13
|
+
repository = "https://github.com/rustic-ai/python-framework"
|
|
14
|
+
packages = [{ include = "rustic_ai", from = "src" }]
|
|
15
|
+
|
|
16
|
+
[tool.poetry.urls]
|
|
17
|
+
"Rustic AI Core" = "https://pypi.org/project/rusticai-core/"
|
|
18
|
+
|
|
19
|
+
[tool.poetry.dependencies]
|
|
20
|
+
python = "^3.12"
|
|
21
|
+
rusticai-core = "^0.0.6"
|
|
22
|
+
rusticai-chroma = "^0.0.3"
|
|
23
|
+
rusticai-langchain = "^0.0.2"
|
|
24
|
+
rusticai-litellm = "^0.0.4"
|
|
25
|
+
rusticai-playwright = "^0.0.2"
|
|
26
|
+
rusticai-serpapi = "^0.0.2"
|
|
27
|
+
|
|
28
|
+
[tool.poetry-monorepo.deps]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
[tool.poetry.group.dev]
|
|
32
|
+
optional = true
|
|
33
|
+
|
|
34
|
+
[tool.poetry.group.dev.dependencies]
|
|
35
|
+
pytest = "^8.3.4"
|
|
36
|
+
black = { version = "^24.2.0", extras = ["jupyter"] }
|
|
37
|
+
flake8 = "^7.1.2"
|
|
38
|
+
tox = "^4.24.1"
|
|
39
|
+
isort = "^6.0.0"
|
|
40
|
+
mypy = "^1.15.0"
|
|
41
|
+
pytest-asyncio = "^0.26.0"
|
|
42
|
+
|
|
43
|
+
[tool.poetry.extras]
|
|
44
|
+
test = ["pytest"]
|
|
45
|
+
|
|
46
|
+
[tool.black]
|
|
47
|
+
line-length = 120
|
|
48
|
+
target-version = ['py312']
|
|
49
|
+
include = '\.pyi?$'
|
|
50
|
+
exclude = '''
|
|
51
|
+
/(
|
|
52
|
+
\.git
|
|
53
|
+
| \.mypy_cache
|
|
54
|
+
| \.tox
|
|
55
|
+
| \.venv
|
|
56
|
+
| _build
|
|
57
|
+
| buck-out
|
|
58
|
+
| build
|
|
59
|
+
| dist
|
|
60
|
+
)/
|
|
61
|
+
'''
|
|
62
|
+
|
|
63
|
+
[tool.mypy]
|
|
64
|
+
python_version = "3.12"
|
|
65
|
+
ignore_missing_imports = true
|
|
66
|
+
check_untyped_defs = true
|
|
67
|
+
plugins = "pydantic.mypy"
|
|
68
|
+
explicit_package_bases = true
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
[tool.isort]
|
|
72
|
+
profile = "black"
|
|
73
|
+
known_third_party = ["pydantic"]
|
|
74
|
+
known_first_party = ["rustic_ai.core"]
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import textwrap
|
|
2
|
+
from typing import Dict
|
|
3
|
+
|
|
4
|
+
import shortuuid
|
|
5
|
+
from pydantic import BaseModel, Field
|
|
6
|
+
from rustic_ai.playwright.agent import WebScrapingCompleted
|
|
7
|
+
from rustic_ai.serpapi.agent import SERPQuery
|
|
8
|
+
|
|
9
|
+
from rustic_ai.core import Agent, AgentSpec, AgentTag
|
|
10
|
+
from rustic_ai.core.agents.indexing.vector_agent import VectorSearchQuery
|
|
11
|
+
from rustic_ai.core.guild import BaseAgentProps, agent
|
|
12
|
+
from rustic_ai.core.guild.agent import ProcessContext
|
|
13
|
+
from rustic_ai.core.guild.agent_ext.depends.llm import LLM
|
|
14
|
+
from rustic_ai.core.guild.agent_ext.depends.llm.models import (
|
|
15
|
+
ChatCompletionRequest,
|
|
16
|
+
SystemMessage,
|
|
17
|
+
TextContentPart,
|
|
18
|
+
UserMessage,
|
|
19
|
+
)
|
|
20
|
+
from rustic_ai.core.guild.agent_ext.depends.vectorstore import VectorSearchResults
|
|
21
|
+
from rustic_ai.core.guild.metaprog.agent_registry import AgentDependency
|
|
22
|
+
from rustic_ai.core.ui_protocol.types import TextFormat
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class UserQuery(BaseModel):
|
|
26
|
+
id: str = Field(default_factory=shortuuid.uuid)
|
|
27
|
+
query: str
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class ResearchUpdates(BaseModel):
|
|
31
|
+
id: str
|
|
32
|
+
query: str
|
|
33
|
+
update: str
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class ResearchManagerConf(BaseAgentProps):
|
|
37
|
+
serp_results: int = Field(default=5)
|
|
38
|
+
vector_results: int = Field(default=5)
|
|
39
|
+
max_serp_attempts: int = Field(default=2)
|
|
40
|
+
context_max: int = Field(default=100000)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class ResearchManager(Agent[ResearchManagerConf]):
|
|
44
|
+
"""
|
|
45
|
+
Agent that manages the research process.
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
def __init__(self, agent_spec: AgentSpec[ResearchManagerConf]):
|
|
49
|
+
super().__init__(agent_spec)
|
|
50
|
+
self.serp_results = agent_spec.props.serp_results
|
|
51
|
+
self.vector_results = agent_spec.props.vector_results
|
|
52
|
+
self.max_serp_attempts = agent_spec.props.max_serp_attempts
|
|
53
|
+
self.context_max = agent_spec.props.context_max
|
|
54
|
+
|
|
55
|
+
self.queries: Dict[str, str] = {}
|
|
56
|
+
self.query_status: Dict[str, str] = {}
|
|
57
|
+
self.query_users: Dict[str, AgentTag] = {}
|
|
58
|
+
self.query_attempts: Dict[str, int] = {}
|
|
59
|
+
|
|
60
|
+
def _process_query(self, query_id: str, query: str, ctx: ProcessContext):
|
|
61
|
+
"""
|
|
62
|
+
Process the user query.
|
|
63
|
+
"""
|
|
64
|
+
self.queries[query_id] = query
|
|
65
|
+
|
|
66
|
+
if ctx.routing_origin.origin_sender:
|
|
67
|
+
self.query_users[query_id] = ctx.routing_origin.origin_sender
|
|
68
|
+
|
|
69
|
+
self.query_attempts[query_id] = 0
|
|
70
|
+
ctx.send(VectorSearchQuery(query=query, id=query_id, k=self.vector_results))
|
|
71
|
+
self.query_status[query_id] = "Searching vector index"
|
|
72
|
+
ctx.send(ResearchUpdates(id=query_id, query=query, update=self.query_status[query_id]))
|
|
73
|
+
|
|
74
|
+
@agent.processor(ChatCompletionRequest)
|
|
75
|
+
def process_user_query(self, ctx: ProcessContext[ChatCompletionRequest]):
|
|
76
|
+
"""
|
|
77
|
+
Run the user query against the vector index.
|
|
78
|
+
"""
|
|
79
|
+
messages = ctx.payload.messages
|
|
80
|
+
user_messages = [msg.content for msg in messages if isinstance(msg, UserMessage)]
|
|
81
|
+
question = ""
|
|
82
|
+
for u_msg in user_messages:
|
|
83
|
+
if isinstance(u_msg, TextContentPart):
|
|
84
|
+
question += u_msg.text
|
|
85
|
+
elif isinstance(u_msg, str):
|
|
86
|
+
question = u_msg
|
|
87
|
+
if len(question) > 0:
|
|
88
|
+
query: UserQuery = UserQuery(query=question)
|
|
89
|
+
self._process_query(query.id, query.query, ctx)
|
|
90
|
+
else:
|
|
91
|
+
ctx.send(TextFormat(text="No results found"))
|
|
92
|
+
|
|
93
|
+
@agent.processor(VectorSearchResults, depends_on=[AgentDependency(dependency_key="llm", guild_level=True)])
|
|
94
|
+
def process_vector_results(self, ctx: ProcessContext[VectorSearchResults], llm: LLM):
|
|
95
|
+
"""
|
|
96
|
+
Process the vector search results.
|
|
97
|
+
"""
|
|
98
|
+
data: VectorSearchResults = ctx.payload
|
|
99
|
+
query_id = data.query_id
|
|
100
|
+
|
|
101
|
+
if data.documents:
|
|
102
|
+
# If vector search had results are found, generate a response using the LLM model
|
|
103
|
+
context = "\n".join([doc.content for doc in data.documents])
|
|
104
|
+
|
|
105
|
+
if len(context) > self.context_max:
|
|
106
|
+
context = context[: self.context_max]
|
|
107
|
+
|
|
108
|
+
self.query_status[query_id] = "Generating response"
|
|
109
|
+
ctx.send(ResearchUpdates(id=query_id, query=self.queries[query_id], update=self.query_status[query_id]))
|
|
110
|
+
|
|
111
|
+
completion_request = ChatCompletionRequest(
|
|
112
|
+
messages=[
|
|
113
|
+
SystemMessage(
|
|
114
|
+
content=textwrap.dedent(
|
|
115
|
+
"""You are a helpful question-answering assistant.
|
|
116
|
+
User will ask you question and will provide you the context of the question.
|
|
117
|
+
Use the context to answer the question. You need to provide the answer to the question based on the context provided.
|
|
118
|
+
If the context doesn't contain the answer, just say that you don't know.
|
|
119
|
+
Use three sentences maximum and keep the answer concise."""
|
|
120
|
+
)
|
|
121
|
+
),
|
|
122
|
+
UserMessage(
|
|
123
|
+
content=f"Question: {data.query}\nContext: {context}",
|
|
124
|
+
name=self.query_users[query_id].id,
|
|
125
|
+
),
|
|
126
|
+
],
|
|
127
|
+
)
|
|
128
|
+
response = llm.completion(completion_request)
|
|
129
|
+
self.query_status[query_id] = "Response generated"
|
|
130
|
+
if response.choices and response.choices[0].message:
|
|
131
|
+
ctx.send(TextFormat(text=response.choices[0].message.content))
|
|
132
|
+
|
|
133
|
+
elif self.query_attempts[query_id] >= self.max_serp_attempts:
|
|
134
|
+
# If the maximum number of serp attempts is reached, return no results
|
|
135
|
+
self.query_status[query_id] = "No results found"
|
|
136
|
+
ctx.send(ResearchUpdates(id=query_id, query=self.queries[query_id], update=self.query_status[query_id]))
|
|
137
|
+
ctx.send(TextFormat(text="No results found"))
|
|
138
|
+
|
|
139
|
+
else:
|
|
140
|
+
# If no results are found, start serp search and scrape the responses
|
|
141
|
+
self.query_attempts[query_id] += 1
|
|
142
|
+
serp_query = SERPQuery(
|
|
143
|
+
engine="google",
|
|
144
|
+
query=self.queries[query_id],
|
|
145
|
+
num=self.serp_results,
|
|
146
|
+
id=query_id,
|
|
147
|
+
start=self.query_attempts[query_id] * self.serp_results,
|
|
148
|
+
)
|
|
149
|
+
ctx.send(serp_query)
|
|
150
|
+
self.query_status[query_id] = "Searching SERP API"
|
|
151
|
+
ctx.send(ResearchUpdates(id=query_id, query=self.queries[query_id], update=self.query_status[query_id]))
|
|
152
|
+
|
|
153
|
+
@agent.processor(WebScrapingCompleted)
|
|
154
|
+
def process_scraping_completion(self, ctx: ProcessContext[WebScrapingCompleted]):
|
|
155
|
+
"""
|
|
156
|
+
Process the scraping completion.
|
|
157
|
+
"""
|
|
158
|
+
data: WebScrapingCompleted = ctx.payload
|
|
159
|
+
query_id = data.id
|
|
160
|
+
query = self.queries[query_id]
|
|
161
|
+
|
|
162
|
+
self.query_status[query_id] = "Scraping completed"
|
|
163
|
+
ctx.send(ResearchUpdates(id=data.id, query=query, update=self.query_status[data.id]))
|
|
164
|
+
ctx.send(VectorSearchQuery(query=query, id=query_id, k=self.vector_results))
|
|
165
|
+
self.query_status[data.id] = "Searching vector index again"
|
|
166
|
+
ctx.send(ResearchUpdates(id=data.id, query=query, update=self.query_status[data.id]))
|