Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dataclass .py tutorial file #270

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions adalflow/tutorials/adalflow_dataclasses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from dataclasses import dataclass, field
from typing import Dict
import adalflow as adal
from adalflow.components.model_client import GroqAPIClient

# Define the QA template using jinja2 syntax
qa_template = r"""<SYS>
You are a helpful assistant.
<OUTPUT_FORMAT>
{{output_format_str}}
</OUTPUT_FORMAT>
</SYS>
<USER> {{input_str}} </USER>"""


# Define the output structure using dataclass
@dataclass
class BasicQAOutput(adal.DataClass):
explanation: str = field(
metadata={"desc": "A brief explanation of the concept in one sentence."}
)
example: str = field(metadata={"desc": "An example of the concept in a sentence."})
__output_fields__ = ["explanation", "example"]


# Define the QA component
class QA(adal.Component):
def __init__(self, model_client: adal.ModelClient, model_kwargs: Dict):
super().__init__()
parser = adal.DataClassParser(data_class=BasicQAOutput, return_data_class=True)
self.generator = adal.Generator(
model_client=model_client,
model_kwargs=model_kwargs,
template=qa_template,
prompt_kwargs={"output_format_str": parser.get_output_format_str()},
output_processors=parser,
)

def call(self, query: str):
"""Synchronous call to generate response"""
return self.generator.call({"input_str": query})

async def acall(self, query: str):
"""Asynchronous call to generate response"""
return await self.generator.acall({"input_str": query})


def run_basic_example():
"""Run a basic example of the QA component"""
qa = QA(
model_client=GroqAPIClient(),
model_kwargs={"model": "llama3-8b-8192"},
)
response = qa("What is LLM?")
print("\nResponse:")
print(response)
print(f"BasicQAOutput: {response.data}")
print(f"Explanation: {response.data.explanation}")
print(f"Example: {response.data.example}")


if __name__ == "__main__":
print("Running basic QA example...")
run_basic_example()
5 changes: 4 additions & 1 deletion docs/source/tutorials/base_data_class.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
<a href="https://colab.research.google.com/github/SylphAI-Inc/AdalFlow/blob/main/notebooks/tutorials/adalflow_dataclasses.ipynb" target="_blank" style="margin-right: 10px;">
<img alt="Try Quickstart in Colab" src="https://colab.research.google.com/assets/colab-badge.svg" style="vertical-align: middle;">
</a>

<a href="https://github.com/SylphAI-Inc/AdalFlow/blob/main/tutorials/adalflow_dataclasses.py" target="_blank" style="display: flex; align-items: center; margin-right: 10px;">
<img src="https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png" alt="GitHub" style="height: 20px; width: 20px; margin-right: 5px;">
<span style="vertical-align: middle;"> Open Source Code</span>
</a>
</div>

DataClass
Expand Down
Loading