-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadvanced_usage.py
More file actions
101 lines (77 loc) · 2.92 KB
/
Copy pathadvanced_usage.py
File metadata and controls
101 lines (77 loc) · 2.92 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
94
95
96
97
98
99
100
101
"""
Advanced usage examples for aigrok.
This module demonstrates advanced features and patterns.
"""
import asyncio
from typing import List, Dict
from aigrok import process_document, process_documents
from aigrok.types import ProcessingResult
async def batch_processing(files: List[str], prompt: str) -> Dict[str, ProcessingResult]:
"""Process multiple documents concurrently.
Args:
files: List of file paths
prompt: Processing prompt
Returns:
Dictionary mapping file paths to results
"""
return await process_documents(files, prompt=prompt, max_concurrent=5)
async def streaming_processing(file_path: str) -> None:
"""Process a document with streaming output."""
async for chunk in process_document(file_path, prompt="Analyze the content", stream=True):
print(f"Chunk: {chunk.text}")
def custom_error_handling():
"""Example with custom error handling."""
try:
process_document("large_file.pdf", prompt="Extract key points", timeout=60, retries=3)
except ValueError as e:
print(f"Invalid parameters: {e}")
except TimeoutError:
print("Processing timed out")
except Exception as e:
print(f"Unexpected error: {e}")
def provider_specific_options():
"""Example using provider-specific options."""
# OpenAI specific
process_document("document.pdf", model="gpt-4", temperature=0.7, max_tokens=2000)
# Anthropic specific
process_document("document.pdf", model="claude-3", max_tokens_to_sample=2000, temperature=0.8)
# Gemini specific
process_document("document.pdf", model="gemini-pro", candidate_count=3, top_k=40)
def custom_output_formatting():
"""Example with custom output formatting."""
# CSV output
process_document("data.pdf", format="csv", columns=["title", "date", "author", "content"])
# Markdown output
process_document("report.pdf", format="markdown", headers=["Summary", "Key Points", "Conclusion"])
# Custom JSON schema
process_document(
"paper.pdf",
format="json",
schema={
"title": {"type": "string", "required": True},
"authors": {"type": "array", "items": {"type": "string"}, "required": True},
"citations": {
"type": "array",
"items": {
"type": "object",
"properties": {
"text": {"type": "string"},
"page": {"type": "integer"},
},
},
},
},
)
async def main():
"""Run all examples."""
# Batch processing
files = ["doc1.pdf", "doc2.pdf", "doc3.pdf"]
await batch_processing(files, "Summarize the content")
# Streaming
await streaming_processing("large_doc.pdf")
# Other examples
custom_error_handling()
provider_specific_options()
custom_output_formatting()
if __name__ == "__main__":
asyncio.run(main())