-
Notifications
You must be signed in to change notification settings - Fork 346
/
tools.py
95 lines (63 loc) · 2.94 KB
/
tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from typing import Literal
import httpx
import marvin
import turbopuffer as tpuf
from prefect import task
from prefect.blocks.system import Secret
from raggy.vectorstores.tpuf import multi_query_tpuf
Topic = Literal["latest_prefect_version"]
@task
async def search_prefect_2x_docs(queries: list[str]) -> str:
"""Searches the Prefect documentation for the given queries.
It is best to use more than one, short query to get the best results.
For example, given a question like:
"Is there a way to get the task_run_id for a task from a flow run?"
You might use the following queries:
- "retrieve task run id from flow run"
- "retrieve run metadata dynamically"
"""
if not tpuf.api_key:
tpuf.api_key = (await Secret.load("tpuf-api-key")).get() # type: ignore
return await multi_query_tpuf(queries, namespace="prefect-2")
@task
async def search_prefect_3x_docs(queries: list[str]) -> str:
"""Searches the Prefect documentation for the given queries.
It is best to use more than one, short query to get the best results.
For example, given a question like:
"Is there a way to get the task_run_id for a task from a flow run?"
You might use the following queries:
- "retrieve task run id from flow run"
"""
if not tpuf.api_key:
tpuf.api_key = (await Secret.load("tpuf-api-key")).get() # type: ignore
return await multi_query_tpuf(queries, namespace="prefect-3")
@task
async def search_controlflow_docs(queries: list[str]) -> str:
"""Searches the ControlFlow documentation for the given queries.
ControlFlow is an agentic framework built on top of Prefect 3.x.
It is best to use more than one, short query to get the best results.
"""
return await multi_query_tpuf(queries, namespace="controlflow")
async def get_latest_prefect_release_notes() -> str:
"""Gets the latest Prefect release notes"""
url = "https://api.github.com/repos/PrefectHQ/prefect/releases/latest"
async with httpx.AsyncClient() as client:
response = await client.get(url)
release_notes = response.json().get("body")
return release_notes
async def get_prefect_code_example(related_to: str) -> str:
"""Gets a Prefect code example"""
base_url = "https://raw.githubusercontent.com/zzstoatzz/prefect-code-examples/main"
async with httpx.AsyncClient() as client:
response = await client.get(f"{base_url}/views/README.json")
example_items = {
item.get("description"): item.get("relative_path")
for category in response.json().get("categories", [])
for item in category.get("examples", [])
}
key = await marvin.classify_async(
data=related_to, labels=list(example_items.keys())
)
best_link = f"{base_url}/{example_items[key]}"
code_example_content = (await client.get(best_link)).text
return f"LINK:\n{best_link}\n\n EXAMPLE:\n{code_example_content}"