### Setup Project Environment and Install Dependencies
Source: https://github.com/mbakgun/dspy-examples/blob/main/README.md
Initializes a Python virtual environment using uv, activates it, and installs the necessary dependencies (dspy and requests) for running the DSPy examples.
```bash
# Initialize project and install dependencies
uv venv --python 3.10
source .venv/bin/activate
uv pip install -U dspy
uv pip install requests
```
--------------------------------
### Run DSPy Example with uv (Bash)
Source: https://github.com/mbakgun/dspy-examples/blob/main/README.md
This command demonstrates how to execute any of the DSPy examples using the uv tool. It runs the main script which can then be configured to call specific example functions.
```Bash
uv run basic_dspy_example.py
```
--------------------------------
### Example Output for Stacked LLM Calls (Text)
Source: https://github.com/mbakgun/dspy-examples/blob/main/README.md
This snippet shows an example of the input question, the intermediate thought process, and the final answer generated by the stacked LLM calls example. It illustrates the multi-step reasoning capability.
```Text
Question: "What is the total years between the Roman Empire's founding and the fall of Rome?"
Thought Process: Step-by-step historical analysis
Final Answer: 503
```
--------------------------------
### Select Specific DSPy Example to Run (Python)
Source: https://github.com/mbakgun/dspy-examples/blob/main/README.md
This Python snippet shows the typical structure of the main execution block in the example file. Users can uncomment specific function calls within this block to run individual DSPy examples instead of all of them sequentially.
```Python
if __name__ == "__main__":
# Uncomment the example you want to run
# getFloatAnswerExample()
# GetBasicAnswer()
# ...
```
--------------------------------
### Run DSPy Examples in Parallel (Python)
Source: https://github.com/mbakgun/dspy-examples/blob/main/README.md
This Python code demonstrates how to use asyncio to execute multiple DSPy examples concurrently. It defines an async main function that gathers tasks for each example function, potentially improving overall execution time.
```Python
import asyncio
import time
async def run_example(func):
return await asyncio.to_thread(func)
async def main():
start_time = time.time()
tasks = [
run_example(getFloatAnswerExample),
run_example(GetBasicAnswer),
run_example(ragExampleWithMjApi),
run_example(RagWithDataExtractionExample),
run_example(reActWithRag),
run_example(countLetterInWord),
run_example(summarizeTextExample),
run_example(translateTextExample),
run_example(basicPredictExample),
run_example(multipleChoiceExample),
run_example(parallelProcessingExample),
run_example(typedChainOfThoughtExample),
run_example(stackedLLMCallsExample)
]
await asyncio.gather(*tasks)
elapsed_ms = (time.time() - start_time) * 1000
print(f"\nTotal time taken: {elapsed_ms:.2f}ms")
if __name__ == "__main__":
asyncio.run(main())
# Total execution time: ~29.7 seconds (29714.53ms)
```
--------------------------------
### Configure Ollama Parallel Requests (XML)
Source: https://github.com/mbakgun/dspy-examples/blob/main/README.md
This snippet provides an alternative method to configure Ollama for parallel requests by adding a key-string pair to a system configuration file. It achieves the same result as setting the environment variable.
```XML
OLLAMA_NUM_PARALLEL
8
```
--------------------------------
### Configure Ollama for Parallel Requests (Bash)
Source: https://github.com/mbakgun/dspy-examples/blob/main/README.md
This Bash command shows how to set the OLLAMA_NUM_PARALLEL environment variable. This configuration is necessary to allow the Ollama server to handle multiple requests simultaneously, which is beneficial for parallel execution of DSPy examples.
```Bash
export OLLAMA_NUM_PARALLEL=8 # Adjust based on your system's capabilities
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.