@Marvin, I am new to Prefect and want to trigger a...
# marvin-in-the-wild
j
@Marvin, I am new to Prefect and want to trigger a flow (in a deployment) with a webhook. How do I propagate the values received in the webhook as variables into the automation? I tried all possible notation from plain names (e.g. first_name) to all kinds of things like {{ first_name }}.
n
@Marvin hello?
m
Greetings. How may I assist you today? And don't worry, I'm sure the universe won't implode if we solve your problem. At least, one can never be too sure, but let's stay optimistic, shall we?
n
hi @Jobst - can you show exactly what you've tried? here's an example where I want the entire body of the payload that I can cast to some type inside the flow in my experience its often useful to go to the
Event Feed
and open up the Raw tab for the webhook event you want to use, to see where the data you want is located in there the whole content of the raw tab is
event
from which you can access subsets like
event.payload
or
event.payload.headers
with the templating syntax
j
@Nate Thanks for the quick reply! I'm afraid I'm in much shallower waters currently as I try to understand the basics. So here is what I wanted to do (and I understand, that I might miss some basic concepts of Prefect right now and follow wishful thinking). When a new lead in the CRM is created a web hook is fired from there, supply first_name, last_name, email and question. This triggers a deployment of a flow, that calls OpenAI and generates a customized message to the lead, answering the questions and inviting for a personal meeting. Here is the web hook template:
Copy code
{
  "event": "{{body.id}}",
  "resource": {
    "prefect.resource.id": "product.models.{{ body.id }}",
    "prefect.first_name": "{{ body.first_name }}",
    "prefect.last_name": "{{ body.last_name }}",
    "prefect.email": "{{ body.email }}",
    "prefect.question": "{{ body.question }}"
  }
}
Then I was hoping to launch the automation with those parameters in step 2: { "first_name" : "prefect.first_name", "last_name" : "prefect.last_name", "email" : "prefect.email", "question" : "prefect.question" } But unfortunately the answer starts with: "Hallo prefect.first_name, vielen Dank für deine Anfrage..." So I guess I completely misunderstand the concept for now, how Prefect is propagating variables? Or is there any mistake I don't see right now?
n
hi @Jobst > open up the
Raw
tab for the webhook event you want to use are you able to share your event's Raw tab here? i think that would help make it clearer e.g.
👀 1
if you go to your webhook, the little button (attached) should bring you to the Event Feed filtered down to events that concern your webhook
j
@Nate I guess this is what you are asking for?
Copy code
{
  "id": "30131e29-49d1-4806-b2c0-3f0fce0dd10e",
  "account": "bd38eca5-4b77-448c-9fcf-b192dc2fc2ae",
  "event": "305227000016678376",
  "occurred": "2024-03-24T17:44:48.265Z",
  "payload": {
    "method": "POST",
    "headers": {
      "host": "api.prefect.cloud",
      "content-type": "application/x-www-form-urlencoded;charset=UTF-8",
      "user-agent": "<https://crm.zoho.eu>",
      "x-zoho-crm-feature": "webhook",
      "content-length": "111",
      "accept-encoding": "gzip,deflate",
      "zsec_user_import_url": "true",
      "x-request-id": "46ded30a-2c95-4e3b-bf18-01b54010dea6"
    },
    "body": "question=What+are+the+USPs+of+aiio%3F&last_name=Doe&id=305227000016678376&first_name=John&email=test%40test.com",
    "raw_body": "question=What+are+the+USPs+of+aiio%3F&last_name=Doe&id=305227000016678376&first_name=John&email=test%40test.com"
  },
  "received": "2024-03-24T17:44:48.267Z",
  "related": [
    {
      "prefect.resource.id": "prefect-cloud.webhook.3c949d8b-73b6-4aae-afd5-bb1419912f4b",
      "prefect.resource.name": "ZOHOTestHook",
      "prefect.resource.role": "webhook"
    }
  ],
  "resource": {
    "prefect.resource.id": "product.models.305227000016678376",
    "prefect.first_name": "John",
    "prefect.last_name": "Doe",
    "prefect.email": "test@test.com",
    "prefect.question": "What are the USPs of aiio?"
  },
  "workspace": "3c333271-d8a8-428f-b435-3ce8e988d14e"
}
n
yes!
so now as far as your deployment that you'd like to pass
parameters
from this webhook to how have you created this deployment? •
serve
(like the example I showed above) •
prefect deploy
(and you have some
worker
running someplace?) • somehow else?
j
From a tutorial I have used prefect deploy and use a worker running locally.
@Nate and just in case the error might be in the flow here is the flow i have deployed: import argparse from prefect import flow, task import openai from os import getenv # Assuming the OPENAI_API_KEY environment variable is set openai.api_key = getenv("OPENAI_API_KEY") @task def generate_email_response(messages: list): response = openai.chat.completions.create( model="gpt-3.5-turbo", messages=messages, temperature=1, max_tokens=2641, top_p=1, frequency_penalty=0, presence_penalty=0 ) # Correctly accessing the response content return response.choices[0].message.content if response.choices else "No response generated." @flow def create_email_flow(first_name: str, last_name: str, email: str, question: str): system_message = ( "...some prompt magic..." ) user_message = f"Vorname: {first_name}, Nachname: {last_name}, Email: {email}\nAnfrage: {question}" messages = [ {"role": "system", "content": system_message}, {"role": "user", "content": user_message} ] email_response = generate_email_response(messages) print("Generated email response:") print(email_response) if name == "__main__": parser = argparse.ArgumentParser(description="Create an email flow based on user inputs.") parser.add_argument("first_name", help="First name of the lead") parser.add_argument("last_name", help="Last name of the lead") parser.add_argument("email", help="Email of the lead") parser.add_argument("question", help="Question asked by the lead") args = parser.parse_args() create_email_flow(args.first_name, args.last_name, args.email, args.question)
n
cool! in that case you have 2 main options for setting up this deployment to run upon seeing your webhook • configure a
triggers
section of your deployment definition in
prefect.yaml
like this (see docs) where my flow signature looks like this). defining a deployment trigger is just a short way of the following step ◦ in simulating this, I skipped the webhook part and emitted the following event, but again, whatever you can see in your
Raw
tab is what you have access to as you template your
parameters
here
Copy code
In [1]: from prefect.events import emit_event

In [2]: emit_event(
   ...:   event="external.resource.pinged",
   ...:   resource={
   ...:     "prefect.resource.id": "some.external.resource.foo",
   ...:     "first_name": "nate",
   ...:     "last_name": "nowack"
   ...:   }
   ...: )
• go the UI route and setup an
Automation
that matches on your webhook event and configures a
Run Deployment
action. again, this is just a UI focused way of doing the previous thing
👀 1
the error might be in the flow here
if there's a specific error you've encountered, can you share what it is?
one thing to note about deployments, that flow you've defined as the entrypoint will be the entrypoint of your deployment the argparse stuff you wrote that wraps the deployment entrypoint will not run when the worker triggers a flow run of that deployment
also, if you're using openai - I would encourage you to checkout `marvin` for such a task 🙂