-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathllm_extraction.py
More file actions
93 lines (70 loc) · 2.89 KB
/
llm_extraction.py
File metadata and controls
93 lines (70 loc) · 2.89 KB
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
"""
LLM-based Extraction Example
This example demonstrates:
- Using natural language prompts to extract data
- LLM automatically generates the extraction workflow
- Support for multiple LLM providers (Ollama, Anthropic, OpenAI)
- Creates a reusable robot that can be executed anytime
- Auto-search: when no URL is provided the system finds the site automatically
Site: Y Combinator Companies (https://www.ycombinator.com/companies)
"""
import asyncio
import json
import os
import sys
from dotenv import load_dotenv
from maxun import Extract, Config
async def main():
extractor = Extract(Config(
api_key=os.environ["MAXUN_API_KEY"],
base_url=os.environ.get("MAXUN_BASE_URL", "https://app.maxun.dev/api/sdk/"),
))
print("Example 1: Creating robot with configured URL...\n")
robot = await extractor.extract(
url="https://www.ycombinator.com/companies",
prompt="Extract the first 15 YC company names, their descriptions, and batch information",
llm_provider="ollama",
llm_model="llama3.2-vision",
llm_base_url="http://localhost:11434",
robot_name="YC Companies LLM Extractor",
)
print(f"Robot created: {robot.id}")
print("Executing robot...\n")
result = await robot.run()
list_data = result.get("data", {}).get("listData") or []
print("Extraction completed!")
print(f" Status: {result.get('status')}")
print(f" Companies extracted: {len(list_data)}\n")
print("First 3 companies:")
print(json.dumps(list_data[:3], indent=2))
print("\n\nExample 2: Creating robot without configured URL...\n")
auto_search_robot = await extractor.extract(
prompt="Extract company names and descriptions from the YCombinator Companies page",
llm_provider="ollama",
robot_name="YC Auto-Search Extractor",
)
print(f"Auto-search robot created: {auto_search_robot.id}")
print("Executing robot...\n")
auto_result = await auto_search_robot.run()
auto_list = auto_result.get("data", {}).get("listData") or []
print("Extraction completed!")
print(f" Status: {auto_result.get('status')}")
print(f" Companies extracted: {len(auto_list)}\n")
print("First 3 companies:")
print(json.dumps(auto_list[:3], indent=2))
# -------------------------------------------------------------------------
# For Anthropic (recommended for best results):
# llm_provider="anthropic",
# llm_model="claude-3-5-sonnet-20241022",
# llm_api_key=os.environ["ANTHROPIC_API_KEY"],
#
# For OpenAI:
# llm_provider="openai",
# llm_model="gpt-4-vision-preview",
# llm_api_key=os.environ["OPENAI_API_KEY"],
# -------------------------------------------------------------------------
load_dotenv()
if not os.environ.get("MAXUN_API_KEY"):
print("Error: MAXUN_API_KEY environment variable is required", file=sys.stderr)
sys.exit(1)
asyncio.run(main())