Skip to content
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
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@ Clone the repo to see syntax highlights using the BAML VSCode extension!

### Requirements

1. BAML CLI, BAML VSCode extension
2. This repository uses ⚠️⚠️⚠️ Poetry ⚠️⚠️⚠️ as the python environment manager. If you want to use Conda, pip or another dependency mgmt, run `baml init` to get yourself setup properly.
3. Python >=3.9 (Contact us if you have an older version).
1. BAML CLI,
2. BAML VSCode extension
3. OPENAI_API_KEY is set in your .env file. See .env.example at the root.

**Contact us on Discord if you need help running the examples using Conda, pip or another dependency mgmt**.

### Setup

Note: You can always just copy the `.baml` files you want into your own project that you have initialized with `baml init`.
We recommend running `baml init` in your own project (unless you just want to clone the NextJS or FastAPI starter projects). Then just copy the .baml files and functions you want.

1. Clone the repo
2. Install [poetry](https://python-poetry.org/docs/)
3. Run `poetry shell` in the root
4. Run `poetry install`
5. Make sure you can ctrl + s one of the .baml files after you install the BAML VSCode extension to generate a baml_client dir.
Make sure you can ctrl + s one of the .baml files after you install the BAML VSCode extension to generate a baml_client dir.

## Troubleshooting

Expand All @@ -30,6 +26,7 @@ Some common steps that help fix things:

1. Make sure you also add `baml` pip package to the project dependencies if you are not using poetry (see pyproject.toml for dependency list).
1. Make sure you're in a poetry shell when you run the python main.py files.
1. Enable Python > Analysis: Type Checking Mode - Basic or greater.
1. Make sure environment variables are set (some baml files use an env.OPEN_AI_KEY). Add a .env file to the root dir with the appropriate api key set to fix this.
1. Restart VScode if the playground isn't working
1. Restart VSCode if you're not getting error highlights for baml-generated code, or ensure the right Python interpreter is set (Command + Shift + P -> Select interpreter)
20 changes: 20 additions & 0 deletions fastapi-starter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Setup

Open this project up in VSCode (we recommend only opening this folder, and not at the root, or VSCode may not detect the python environment and you may not get type completions for BAML functions).

Ensure your `settings.json` has:

```
{
"python.analysis.typeCheckingMode": "basic"
}
```

1. Run `poetry install`
2. Run `poetry shell`
3. Open up vscode command palette (command + shift + p, and select the .venv folder that was created in this directory as the interpreter)
4. Run `uvicorn fastapi_starter.app:app --reload`
5. Curl the streaming endpoint:
```
curl -X GET -H "Content-Type: application/json" http://localhost:8000/extract_resume
```
46 changes: 46 additions & 0 deletions fastapi-starter/baml-README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Getting started with BAML

## Installations

Make sure to download the [VSCode Playground](https://marketplace.visualstudio.com/items?itemName=gloo.baml).

To use BAML with either python or typescript, you should run:

```shell
$ baml update-client
```

This will keep client side libraries in sync. It also prints the commands being run, which you can run manually if they fail.

## Running tests

You can run tests via:

```shell
# To run tests
$ baml test run

# To list tests
$ baml test

# For more help
$ baml test --help
```

## Integrating BAML with python / ts

You can run:

```shell
$ python -m baml_example_app
```

The `baml_example_app.py` file shows how to import from the code BAML generates.

## Deploying

You don't need the BAML compiler when you deploy / release. Your `baml_client` folder contains everything you may need.

## Reporting bugs

Report any issues on our [Github](https://www.github.com/boundaryml/baml) or [Discord](https://discord.gg/BTNBeXGuaS)
100 changes: 100 additions & 0 deletions fastapi-starter/baml_example_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"""
Run this script to see how the BAML client can be used in Python.

python -m example_baml_app
"""

import asyncio
from baml_client import baml as b
from datetime import datetime
from typing import List
from typing_extensions import TypedDict

async def extract_resume(resume: str) -> None:
"""
Extracts the resume and prints the extracted data.
"""
print("Parsing resume...")
print(resume[:100] + "..." if len(resume) > 100 else resume)
parsed_resume = await b.ExtractResume(resume)
print(parsed_resume.model_dump_json(indent=2))

await asyncio.sleep(1)
print("\n\nNow extracting using streaming")
async with b.ExtractResume.stream(resume) as stream:
async for x in stream.parsed_stream:
if x.is_parseable:
print(f"streaming: {x.parsed.model_dump_json()}")
response = await stream.get_final_response()
if response.has_value:
print(f"\n final: {response.value.model_dump_json(indent=2)}")
else:
print("No final response")


class ChatMessage(TypedDict):
sender: str
message: str


async def classify_chat(messages: List[ChatMessage]) -> None:
"""
Classifies the chat and prints the classification.
"""
print("Classifying chat...")
chat = "\n".join(map(lambda m: f'{m["sender"]}: {m["message"]}', messages))
print(chat[:100] + "..." if len(chat) > 100 else chat)

classification = await b.ClassifyMessage(
message=chat, message_date=datetime.now().strftime("%Y-%m-%d")
)
print("Got categories: ", classification)


async def main():
resume = """
John Doe
1234 Elm Street
Springfield, IL 62701
(123) 456-7890

Objective: To obtain a position as a software engineer.

Education:
Bachelor of Science in Computer Science
University of Illinois at Urbana-Champaign
May 2020 - May 2024

Experience:
Software Engineer Intern
Google
May 2022 - August 2022
- Worked on the Google Search team
- Developed new features for the search engine
- Wrote code in Python and C++

Software Engineer Intern
Facebook
May 2021 - August 2021
- Worked on the Facebook Messenger team
- Developed new features for the messenger app
- Wrote code in Python and Java
"""
await extract_resume(resume)

messages = [
{"sender": "Alice", "message": "I'm having issues with my computer."},
{
"sender": "Assistant",
"message": "I'm sorry to hear that. What seems to be the problem?",
},
{
"sender": "Alice",
"message": "It's running really slow. I need to return it. Can I get a refund?",
},
]
await classify_chat(messages)


if __name__ == "__main__":
asyncio.run(main())
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"input": {
"message": "This is so frustrating, i bought a laptop and it's not working properly. I want to return it and get my money back. I'm so disappointed",
"message_date": "2019-01-01T00:00:00Z"
}
}
6 changes: 6 additions & 0 deletions fastapi-starter/baml_src/__tests__/ClassifyMessage/case1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"input": {
"message": "Hi! I'm having an issue with my account. Can you help me?",
"message_date": "2019-01-01T00:00:00Z"
}
}
3 changes: 3 additions & 0 deletions fastapi-starter/baml_src/__tests__/ExtractResume/jason.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"input": "Jason Doe\nPython, Rust\nUniversity of California, Berkeley, B.S.\nin Computer Science, 2020\nAlso an expert in Tableau, SQL, and C++\n"
}
3 changes: 3 additions & 0 deletions fastapi-starter/baml_src/__tests__/ExtractResume/sarah.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"input": "Sarah Montez\nHarvard University\nMay 2015-2019\n3.92 GPA\nGoogle\nSoftware Engineer\nJune 2019-Present\n- Backend engineer\n- Rewrote search and uplifted metrics by 120%\n- Used C++ and Python\nMicrosoft\nSoftware Intern\nJune 2018-August 2018\n- Worked on the Windows team\n- Updated the UI\n- Used C++\n"
}
23 changes: 23 additions & 0 deletions fastapi-starter/baml_src/clients.baml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
client<llm> GPT4 {
provider baml-openai-chat
options {
model gpt-4
api_key env.OPENAI_API_KEY
}
}

client<llm> GPT4Turbo {
provider baml-openai-chat
options {
model gpt-4-1106-preview
api_key env.OPENAI_API_KEY
}
}

client<llm> GPT3 {
provider baml-openai-chat
options {
model gpt-3.5-turbo
api_key env.OPENAI_API_KEY
}
}
33 changes: 33 additions & 0 deletions fastapi-starter/baml_src/example_1.baml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Resume {
name string
education Education[]
skills string[]
}

class Education {
school string
degree string
year int
}

function ExtractResume {
input string
output Resume
}

impl<llm, ExtractResume> version1 {
client GPT4
prompt #"
Parse the following resume and return a structured representation of the data in the schema below.

Resume:
---
{#input}
---

Output JSON format (only include these fields, and no others):
{#print_type(output)}

Output JSON:
"#
}
28 changes: 28 additions & 0 deletions fastapi-starter/baml_src/example_2.baml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
enum Category {
Refund
CancelOrder
TechnicalSupport
AccountIssue
Question
}

function ClassifyMessage {
input (message: string, message_date: string)
output Category[]
}

impl<llm, ClassifyMessage> level1 {
client GPT4
prompt #"
Classify the following INPUT into following:
{#print_enum(Category)}

INPUT
---
date: {#input.message_date}
message: {#input.message}
---

JSON array of categories that match:
"#
}
13 changes: 13 additions & 0 deletions fastapi-starter/baml_src/main.baml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
generator lang_python {
language python
// This is where your baml_client will be generated
// Usually the root of your source code relative to this file
project_root "../"
// This command is used by "baml test" to run tests
// defined in the playground
test_command "poetry run pytest"
// This command is used by "baml update-client" to install
// dependencies to your language environment
install_command "poetry add baml@latest"
package_version_command "poetry show baml"
}
53 changes: 53 additions & 0 deletions fastapi-starter/fast_api_starter/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from fastapi import FastAPI
import os
from baml_client import baml as b

from fastapi.responses import StreamingResponse


app = FastAPI()

@app.get("/")
def index():
return {"Hello": "World"}

@app.get("/extract_resume")
async def extract_resume():

resume = """
John Doe
1234 Elm Street
Springfield, IL 62701
(123) 456-7890

Objective: To obtain a position as a software engineer.

Education:
Bachelor of Science in Computer Science
University of Illinois at Urbana-Champaign
May 2020 - May 2024

Experience:
Software Engineer Intern
Google
May 2022 - August 2022
- Worked on the Google Search team
- Developed new features for the search engine
- Wrote code in Python and C++

Software Engineer Intern
Facebook
May 2021 - August 2021
- Worked on the Facebook Messenger team
- Developed new features for the messenger app
- Wrote code in Python and Java
"""
async def stream_resume(resume):
async with b.ExtractResume.stream(resume) as stream:
async for chunk in stream.parsed_stream:
print(chunk.delta)
if chunk.is_parseable:
yield str(chunk.parsed.model_dump_json()) + "\n"

return StreamingResponse(stream_resume(resume), media_type="text/plain")

Loading