OpenAIEmbeddingOperator¶
Use the OpenAIEmbeddingOperator to
interact with the OpenAI API to create embeddings for given text.
Using the Operator¶
The OpenAIEmbeddingOperator requires the input_text as an input to embedding API. Use the conn_id parameter to specify the OpenAI connection to use to
connect to your account.
An example of using the operator:
OpenAIEmbeddingOperator(
task_id="embedding_using_xcom_data",
conn_id="openai_default",
input_text=task_to_store_input_text_in_xcom(),
model="text-embedding-3-small",
)
OpenAIEmbeddingOperator(
task_id="embedding_using_callable",
conn_id="openai_default",
input_text=input_text_callable(
"input_arg1_value",
"input2_value",
input_kwarg1="input_kwarg1_value",
input_kwarg2="input_kwarg2_value",
),
model="text-embedding-3-small",
)
OpenAIEmbeddingOperator(
task_id="embedding_using_text",
conn_id="openai_default",
input_text=texts,
model="text-embedding-3-small",
)
OpenAIResponseOperator¶
Use the OpenAIResponseOperator to generate a
model response with the OpenAI Responses API, OpenAI’s recommended interface for text generation and
tool use. The operator returns the response’s aggregated output text.
Using the Operator¶
The OpenAIResponseOperator requires the input_text prompt. Use the conn_id parameter to
specify the OpenAI connection to use, and response_kwargs to pass through options such as
tools, conversation or previous_response_id.
OpenAIResponseOperator(
task_id="openai_response",
conn_id="openai_default",
input_text="Write a haiku about data pipelines.",
response_kwargs={"instructions": "You are a helpful assistant."},
)
Using the OpenAIHook for Responses and Conversations¶
The OpenAIHook exposes the Responses and
Conversations APIs directly for use inside @task functions or custom operators:
Responses:
create_response,get_response,delete_responseandcancel_response(the last cancels a response created withbackground=True).Conversations:
create_conversation,get_conversation,update_conversationanddelete_conversation. Pass the conversation id tocreate_response(via the operator’sresponse_kwargsor the hook) to persist state across responses.
For example, to create a conversation and continue it across responses:
hook = OpenAIHook()
conversation = hook.create_conversation()
hook.create_response(input="Hello", conversation=conversation.id)
Note
The Assistants/Threads hook methods (create_assistant, create_thread, create_run and
related) are deprecated, mirroring OpenAI’s deprecation of the Assistants API. Migrate to the
Responses and Conversations methods above.
OpenAITriggerBatchOperator¶
Use the OpenAITriggerBatchOperator to
interact with the OpenAI API to trigger a batch job. This operator is used to trigger a batch job and wait for the job to complete.
Using the Operator¶
The OpenAITriggerBatchOperator requires the prepared batch file as an input to trigger the
batch job. Provide the file_id and the endpoint to trigger the batch job, and use the
conn_id parameter to specify the OpenAI connection to use.
An example of using the operator:
from airflow.providers.openai.operators.openai import OpenAITriggerBatchOperator
batch_id = OpenAITriggerBatchOperator(
task_id="batch_operator_deferred",
conn_id=OPENAI_CONN_ID,
file_id=batch_file_id,
endpoint="/v1/chat/completions",
deferrable=True,
)