Skip to content

Commit 6bac239

Browse files
authored
update cheatsheet (#8538)
* update cheatsheet * use title style
1 parent f091509 commit 6bac239

File tree

1 file changed

+105
-1
lines changed

1 file changed

+105
-1
lines changed

docs/docs/cheatsheet.md

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ print(f"Question: {question}")
4040
print(f"Final Predicted Answer (after ProgramOfThought process): {result.answer}")
4141
```
4242

43-
### dspy.ReACT
43+
### dspy.ReAct
4444

4545
```python
4646
react_module = dspy.ReAct(BasicQA)
@@ -70,6 +70,38 @@ for idx, passage in enumerate(topK_passages):
7070
print(f'{idx+1}]', passage, '\n')
7171
```
7272

73+
### dspy.CodeAct
74+
75+
```python
76+
from dspy import CodeAct
77+
78+
def factorial(n):
79+
"""Calculate factorial of n"""
80+
if n == 1:
81+
return 1
82+
return n * factorial(n-1)
83+
84+
act = CodeAct("n->factorial", tools=[factorial])
85+
result = act(n=5)
86+
result # Returns 120
87+
```
88+
89+
### dspy.Parallel
90+
91+
```python
92+
import dspy
93+
94+
parallel = dspy.Parallel(num_threads=2)
95+
predict = dspy.Predict("question -> answer")
96+
result = parallel(
97+
[
98+
(predict, dspy.Example(question="1+1").with_inputs("question")),
99+
(predict, dspy.Example(question="2+2").with_inputs("question"))
100+
]
101+
)
102+
result
103+
```
104+
73105
## DSPy Metrics
74106

75107
### Function as Metric
@@ -355,6 +387,78 @@ optimized_program = simba.compile(student=your_dspy_program, trainset=trainset)
355387
```
356388

357389

390+
## DSPy Tools and Utilities
391+
392+
### dspy.Tool
393+
394+
```python
395+
import dspy
396+
397+
def search_web(query: str) -> str:
398+
"""Search the web for information"""
399+
return f"Search results for: {query}"
400+
401+
tool = dspy.Tool(search_web)
402+
result = tool(query="Python programming")
403+
```
404+
405+
### dspy.streamify
406+
407+
```python
408+
import dspy
409+
import asyncio
410+
411+
predict = dspy.Predict("question->answer")
412+
413+
stream_predict = dspy.streamify(
414+
predict,
415+
stream_listeners=[dspy.streaming.StreamListener(signature_field_name="answer")],
416+
)
417+
418+
async def read_output_stream():
419+
output_stream = stream_predict(question="Why did a chicken cross the kitchen?")
420+
421+
async for chunk in output_stream:
422+
print(chunk)
423+
424+
asyncio.run(read_output_stream())
425+
```
426+
427+
428+
### dspy.asyncify
429+
430+
```python
431+
import dspy
432+
433+
dspy_program = dspy.ChainOfThought("question -> answer")
434+
dspy_program = dspy.asyncify(dspy_program)
435+
436+
asyncio.run(dspy_program(question="What is DSPy"))
437+
```
438+
439+
440+
### Track Usage
441+
442+
```python
443+
import dspy
444+
dspy.settings.configure(track_usage=True)
445+
446+
result = dspy.ChainOfThought(BasicQA)(question="What is 2+2?")
447+
print(f"Token usage: {result.get_lm_usage()}")
448+
```
449+
450+
### dspy.configure_cache
451+
452+
```python
453+
import dspy
454+
455+
# Configure cache settings
456+
dspy.configure_cache(
457+
enable_disk_cache=False,
458+
enable_memory_cache=False,
459+
)
460+
```
461+
358462
## DSPy `Refine` and `BestofN`
359463

360464
>`dspy.Suggest` and `dspy.Assert` are replaced by `dspy.Refine` and `dspy.BestofN` in DSPy 2.6.

0 commit comments

Comments
 (0)