import weave
from openai import OpenAI
client = OpenAI()
# Weave tracks the inputs, outputs and code of this function
@weave.op()
def extract_dinos(sentence):
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "system",
"content": """Extract dinosaurs from the text. Return JSON with:
- dinosaurs: list of dinosaurs
- name: scientific name
- common_name: nickname
- diet: herbivore or carnivore."""
},
{
"role": "user",
"content": sentence
}
],
response_format={ "type": "json_object" }
)
return response.choices[0].message.content
# Start the weave project
weave.init('jurassic-park')
sentence = """I saw a T. rex chase a Triceratops.
The T. rex eats meat. The Triceratops eats plants.
A Brachiosaurus ate leaves from tall trees nearby."""
result = extract_dinos(sentence)
print(result)