← All ivy-nodes
IVYXSTUDIO · IVY NODE
O
OpenAI Client
ivy.node.open-ai-client · v0.0.0
ivyx✓
Generates text responses using OpenAI's chat completion API. Supports various models including GPT-4, GPT-4o, and o1 series. This node requires an OpenAI API key and accepts both a prompt object (from prompt-generator) and direct system/user messages. Use this for high-quality text generation, summarization, or conversational AI tasks. Note: This is a high-risk node requiring step-by-step approval due to API costs.
#OpenAI#API#Chat Completion
Inputs
| Field | Type | Description |
|---|---|---|
| modelrequired | string | The OpenAI model to use (e.g., "gpt-4o-mini"). |
| api_keyrequired | string | The API key for authenticating with OpenAI. |
| promptrequired | string | The prompt content. (Node reference) |
| system_messagerequired | string | The system's initial message for context. |
| user_contentrequired | string | The user's input content. |
| features | object | Features |
| labels | object | Labels |
Outputs
| Field | Type | Description |
|---|---|---|
| openai_resultrequired | string | Not declared |
| features | object | Features |
| labels | object | Labels |
Source
python
# Input preparation
inp = __ivy_ctx__["nodes"][__ivy_node_id__]["input"]
model = inp["model"]
api_key = inp["api_key"]
prompt = inp["prompt"]
system_message = inp["system_message"]
user_content = inp["user_content"]
# Compute
from openai import OpenAI
class OpenAIClient:
"""
A class to interact with OpenAI's API for generating chat completions.
"""
def __init__(self, api_key: str):
"""
Initialize the OpenAIClient with the provided API key.
:param api_key: The API key for authenticating with OpenAI.
"""
self.api_key=api_key
self.client=OpenAI(api_key=api_key)
def generate_response(self, model: str, system_message: str, user_content: str) -> str:
"""
Generate a chat completion response using the specified model and messages.
:param model: The OpenAI model to use (e.g., "gpt-4o-mini").
:param system_message: The system's initial message for context.
:param user_content: The user's input content.
:return: The response content generated by OpenAI.
"""
response=self.client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": system_message},
{"role": "user", "content": user_content},
]
)
return response.choices[0].message.content
openai_client = OpenAIClient(api_key=api_key)
openai_result = openai_client.generate_response(
model=model,
system_message=system_message,
user_content=user_content
)
# Output collection (runner reads __ivy_ctx__)
out = __ivy_ctx__["nodes"][__ivy_node_id__]["output"]
out["openai_result"] = openai_resultTests
Requires: python:3.11
- basic-generation
Generate response with valid inputs (should succeed).
openaillmgenerationbasic - error-invalid-api-key
Invalid API key should handle error gracefully.
error-handlingauthentication