-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdebug_example.py
More file actions
63 lines (51 loc) · 1.93 KB
/
debug_example.py
File metadata and controls
63 lines (51 loc) · 1.93 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
#!/usr/bin/env python3
import sys
sys.path.insert(0, '..')
import funnydspy as fd
import dspy
from typing import List, Literal
# Configure DSPy
dspy.configure(lm=dspy.LM('openai/gpt-4.1-nano', cache=False))
@fd.Predict
def gist_producer(parent_headings: List[str], chunk: str) -> str: return gist
@fd.ChainOfThought
def header_producer(parent_headings: List[str], chunk_gists: List[str]) -> List[str]: return content_headings
print("Testing gist_producer in parallel...")
parent_headings = ['Test']
chunks = ['chunk1', 'chunk2']
chunk_gists = fd.parallel(gist_producer, [
{'parent_headings': parent_headings, 'chunk': c} for c in chunks
])
print(f"chunk_gists: {chunk_gists}")
print(f"types: {[type(x) for x in chunk_gists]}")
print("\nTesting header_producer...")
headers = header_producer(parent_headings, chunk_gists)
print(f"headers: {headers}")
print(f"type: {type(headers)}")
# Test the classifier creation
print(f"\nCreating classifier with headers: {headers}")
@fd.ChainOfThought
def classifier(parent_headings: List[str], chunk: str) -> Literal[*headers]: return topic
print("Testing classifier in parallel...")
topics = fd.parallel(classifier, [
{'parent_headings': parent_headings, 'chunk': c} for c in chunks
])
print(f"topics: {topics}")
print(f"types: {[type(x) for x in topics]}")
# Test if topics can be used as dictionary keys
print(f"\nTesting dictionary usage...")
sections = {topic: [] for topic in headers}
print(f"sections dict created: {sections}")
try:
for topic, chunk in zip(topics, chunks):
print(f"Checking if {topic} (type: {type(topic)}) is in sections...")
if topic in sections:
sections[topic].append(chunk)
print(f"Added {chunk} to {topic}")
else:
print(f"Topic {topic} not in sections keys: {list(sections.keys())}")
print(f"Final sections: {sections}")
except Exception as e:
print(f"Error: {e}")
import traceback
traceback.print_exc()