Skip to content

Commit

Permalink
Merge pull request #1080 from Agenta-AI/feat/templates
Browse files Browse the repository at this point in the history
Add async version of baby name generator app
  • Loading branch information
mmabrouk authored Dec 19, 2023
2 parents 286ec84 + 4aabaa8 commit efc12ee
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/baby_name_generator/app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from agenta import FloatParam, TextParam
import agenta as ag
from openai import OpenAI

client = OpenAI()
from agenta import FloatParam, TextParam

default_prompt = (
"Give me 10 names for a baby from this country {country} with gender {gender}!!!!"
Expand Down
34 changes: 34 additions & 0 deletions examples/baby_name_generator/app_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from agenta import FloatParam, TextParam
import agenta as ag
from openai import AsyncOpenAI

client = AsyncOpenAI()

default_prompt = (
"Give me 10 names for a baby from this country {country} with gender {gender}!!!!"
)

ag.init()
ag.config.default(
temperature=FloatParam(0.2), prompt_template=TextParam(default_prompt)
)


@ag.entrypoint
async def generate(country: str, gender: str) -> str:
"""
Generate a baby name based on the given country and gender.
Args:
country (str): The country to generate the name from.
gender (str): The gender of the baby.
Returns:
str: The generated baby name.
"""
prompt = ag.config.prompt_template.format(country=country, gender=gender)

chat_completion = await client.chat.completions.create(
model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}]
)
return chat_completion.choices[0].message.content
52 changes: 46 additions & 6 deletions examples/job_info_extractor/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,49 @@
# Using this template
# Extraction using OpenAI Functions and Langchain"

Please make sure to create a `.env` file with your OpenAI API key before running the app.
OPENAI_API_KEY=sk-xxxxxxx

You can find your keys here:
https://platform.openai.com/account/api-keys
This templates is designed to extracts job information (company name, job
title, salary range) from a job description. It uses OpenAI Functions and
Langchain. It runs with agenta.
[Agenta](https://github.com/agenta-ai/agenta) is an open-source LLMOps
platform that allows you to 1) quickly experiment and compare
configuration for LLM apps 2) evaluate prompts and workflows 3) deploy
applications easily.

## How to use
### 0. Prerequisites
- Install the agenta CLI
```bash
pip install agenta-cli
```
- Either create an account in [agenta cloud](https://cloud.agenta.ai/) or
[self-host agenta](/self-host/host-locally)

### 1. Clone the repository

```bash
git clone https://github.com/Agenta-AI/job_extractor_template
```

### 2. Initialize the project

```bash
agenta init
```

### 3. Setup your openAI API key
Create a .env file by copying the .env.example file and add your openAI
API key to it.
```bash
OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxx
```

### 4. Deploy the application to agenta

```bash
agenta variant serve app.py
```

### 5. Experiment with the prompts in a playground and evaluate different variants in agenta

https://github.com/Agenta-AI/job_extractor_template/assets/4510758/30271188-8d46-4d02-8207-ddb60ad0e284

Go back to the [Getting started tutorial](https://docs.agenta.ai/getting-started) to continue
17 changes: 12 additions & 5 deletions examples/job_info_extractor/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@

from pydantic import BaseModel, Field

default_prompt = "What is a good name for a company that makes {product}?"
CHAT_LLM_GPT = [
"gpt-3.5-turbo-16k-0613",
"gpt-3.5-turbo-16k",
"gpt-3.5-turbo-0613",
"gpt-3.5-turbo-0301",
"gpt-3.5-turbo",
"gpt-4",
]

ag.init()
ag.config.default(
prompt_template=ag.TextParam(default_prompt),
system_message=ag.TextParam(
"You are a world class algorithm for extracting information in structured formats."
),
Expand All @@ -26,10 +32,11 @@
company_desc_message=ag.TextParam("The name of the company"),
position_desc_message=ag.TextParam("The name of the position"),
salary_range_desc_message=ag.TextParam("The salary range of the position"),
temperature=ag.FloatParam(0.5),
top_p=ag.FloatParam(1.0),
temperature=ag.FloatParam(0.9),
top_p=ag.FloatParam(0.9),
presence_penalty=ag.FloatParam(0.0),
frequency_penalty=ag.FloatParam(0.0),
model=ag.MultipleChoiceParam("gpt-3.5-turbo-0613", CHAT_LLM_GPT),
)


Expand All @@ -50,7 +57,7 @@ def generate(
) -> str:
"""Extract information from a job description"""
llm = ChatOpenAI(
model="gpt-3.5-turbo-0613",
model=ag.config.model,
temperature=ag.config.temperature,
top_p=ag.config.top_p,
presence_penalty=ag.config.presence_penalty,
Expand Down

0 comments on commit efc12ee

Please sign in to comment.