|
| 1 | +# ruff: noqa: E501 |
| 2 | + |
| 3 | +from openai import OpenAI |
| 4 | + |
| 5 | +from alphatrion.tracing.tracing import task, workflow |
| 6 | + |
| 7 | +client = OpenAI( |
| 8 | + base_url="http://localhost:11434/v1", |
| 9 | + api_key="", |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +@task(name="joke_creation") |
| 14 | +def create_joke(): |
| 15 | + completion = client.chat.completions.create( |
| 16 | + model="smollm:135m", |
| 17 | + messages=[{"role": "user", "content": "Tell me a joke about opentelemetry"}], |
| 18 | + ) |
| 19 | + return completion.choices[0].message.content |
| 20 | + |
| 21 | + |
| 22 | +@task(name="joke_translation") |
| 23 | +def translate_joke_to_pirate(joke: str): |
| 24 | + completion = client.chat.completions.create( |
| 25 | + model="smollm:135m", |
| 26 | + messages=[ |
| 27 | + { |
| 28 | + "role": "user", |
| 29 | + "content": f"Translate the below joke to pirate-like english:\n\n{joke}", |
| 30 | + } |
| 31 | + ], |
| 32 | + ) |
| 33 | + return completion.choices[0].message.content |
| 34 | + |
| 35 | + |
| 36 | +@task(name="signature_generation") |
| 37 | +def generate_signature(joke: str): |
| 38 | + completion = client.chat.completions.create( |
| 39 | + model="smollm:135m", |
| 40 | + messages=[ |
| 41 | + {"role": "user", "content": "add a signature to the joke:\n\n" + joke} |
| 42 | + ], |
| 43 | + ) |
| 44 | + return completion.choices[0].message.content |
| 45 | + |
| 46 | + |
| 47 | +@workflow(name="pirate_joke_generator") |
| 48 | +def joke_workflow(): |
| 49 | + eng_joke = create_joke() |
| 50 | + pirate_joke = translate_joke_to_pirate(eng_joke) |
| 51 | + signature = generate_signature(pirate_joke) |
| 52 | + return pirate_joke, signature |
| 53 | + |
| 54 | + |
| 55 | +def test_workflow(): |
| 56 | + pirate_joke, signature = joke_workflow() |
| 57 | + assert pirate_joke is not None |
| 58 | + assert signature is not None |
| 59 | + |
| 60 | + |
| 61 | +if __name__ == "__main__": |
| 62 | + test_workflow() |
0 commit comments