* Fix: Fix Setup lint GitHub action #30 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Remove branch filter on PR and remove on push Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Remove on mergequeue Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: Add tox.ini file Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Return on push Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Chore: tox -e ruff Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Ignore .ruff_cache Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Chore: Update tox file Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Chore: Add mypy.ini Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Ignore if line is too long Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: Set the ignore in command instead Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: W503 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: F541 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: 541 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: W503 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: F541 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Chore: Ignore F401, unused imports as __init__ files has them Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Return linebreak as tox -e ruff yields that Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Ignore W503 as ruff introduces it Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: F841 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: E203 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: W293 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: W291 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: F541 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: E203 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: E203 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: W291 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: F541 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: F811 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: F841 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: F811 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: F541 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: F541 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: F841 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: F811 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: W291 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: F811 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: F541 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: F541 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Ruff want's the space before : Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Ignore space before : Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: E291 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: Add dev-requirements.txt Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: Correct python version Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: Ref dev-requirements.txt Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: Add mypy cache dir Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Chore: Update mypy version Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Fix: Exclude cookbook and quicstarts Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Remove unused import Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Chore: Add specific sub module ignore on error for future smaller fixing Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Reintroduce branches filter on push and pull_request Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * chore: Ruff Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Chore: ruff formatting * Chore: F541 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Chore: E401 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Chore: Ruff Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Chore: F811 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Chore: F841 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Chore: Ruff Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Chore: E711 Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> * Chore: ruff Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> --------- Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com> |
||
---|---|---|
.. | ||
README.md | ||
audio_transcription.py | ||
audio_translation.py | ||
basic.prompty | ||
embeddings.py | ||
requirements.txt | ||
speech.mp3 | ||
speech_spanish.mp3 | ||
structured_completion.py | ||
text_completion.py | ||
text_to_speech.py |
README.md
OpenAI LLM calls with Dapr Agents
This quickstart demonstrates how to use Dapr Agents' LLM capabilities to interact with language models and generate both free-form text and structured data. You'll learn how to make basic calls to LLMs and how to extract structured information in a type-safe manner.
Prerequisites
- Python 3.10 (recommended)
- pip package manager
- OpenAI API key
Environment Setup
# Create a virtual environment
python3.10 -m venv .venv
# Activate the virtual environment
# On Windows:
.venv\Scripts\activate
# On macOS/Linux:
source .venv/bin/activate
# Install dependencies
pip install -r requirements.txt
Configuration
Create a .env
file in the project root:
OPENAI_API_KEY=your_api_key_here
Replace your_api_key_here
with your actual OpenAI API key.
Examples
Text
1. Run the basic text completion example:
python text_completion.py
The script demonstrates basic usage of Dapr Agents' OpenAIChatClient for text generation:
from dapr_agents import OpenAIChatClient
from dapr_agents.types import UserMessage
from dotenv import load_dotenv
# Load environment variables from .env
load_dotenv()
# Basic chat completion
llm = OpenAIChatClient()
response = llm.generate("Name a famous dog!")
if len(response.get_content()) > 0:
print("Response: ", response.get_content())
# Chat completion using a prompty file for context
llm = OpenAIChatClient.from_prompty('basic.prompty')
response = llm.generate(input_data={"question":"What is your name?"})
if len(response.get_content()) > 0:
print("Response with prompty: ", response.get_content())
# Chat completion with user input
llm = OpenAIChatClient()
response = llm.generate(messages=[UserMessage("hello")])
if len(response.get_content()) > 0 and "hello" in response.get_content().lower():
print("Response with user input: ", response.get_content())
2. Expected output: The LLM will respond with the name of a famous dog (e.g., "Lassie", "Hachiko", etc.).
Run the structured text completion example:
python structured_completion.py
This example shows how to use Pydantic models to get structured data from LLMs:
import json
from dapr_agents import OpenAIChatClient
from dapr_agents.types import UserMessage
from pydantic import BaseModel
from dotenv import load_dotenv
# Load environment variables from .env
load_dotenv()
# Define our data model
class Dog(BaseModel):
name: str
breed: str
reason: str
# Initialize the chat client
llm = OpenAIChatClient()
# Get structured response
response = llm.generate(
messages=[UserMessage("One famous dog in history.")],
response_format=Dog
)
print(json.dumps(response.model_dump(), indent=2))
Expected output: A structured Dog object with name, breed, and reason fields (e.g., Dog(name='Hachiko', breed='Akita', reason='Known for his remarkable loyalty...')
)
Audio
You can use the OpenAIAudioClient in dapr-agents
for basic tasks with the OpenAI Audio API. We will explore:
- Generating speech from text and saving it as an MP3 file.
- Transcribing audio to text.
- Translating audio content to English.
1. Run the text to speech example:
python text_to_speech.py
2. Run the speech to text transcription example:
python audio_transcription.py
2. Run the speech to text translation example:
Embeddings
You can use the OpenAIEmbedder
in dapr-agents for generating text embeddings.
1. Embeddings a single text:
python embeddings.py
Troubleshooting
- Authentication Errors: If you encounter authentication failures, check your OpenAI API key in the
.env
file - Structured Output Errors: If the model fails to produce valid structured data, try refining your model or prompt
- Module Not Found: Ensure you've activated your virtual environment and installed the requirements
Next Steps
After completing these examples, move on to the Agent Tool Call quickstart to learn how to build agents that can use tools to interact with external systems.