Published and effective on: Jan 27, 2025
Oracle provides a RESTful API to handle user conversations seamlessly. The /chat endpoint interacts with AI agents, maintains chat history, and gracefully handles errors.
@app.route("/chat", methods=["POST"])
def chat():
data = request.json
agent_index = data.get("agentIndex", 0)
user_message = data.get("message", "").strip()
if not user_message:
return jsonify({"error": "Empty message"}), 400
agent = get_agent(agent_index)
persist_chat(agent_index, "user", user_message)
bot_reply = fetch_openai_response(f"You are a {agent['traits']} assistant.", user_message)
persist_chat(agent_index, "bot", bot_reply)
return jsonify({"reply": bot_reply, "history": chat_history[agent_index]})
Oracle offers APIs for managing AI agents, enabling seamless customization and management.
@app.route('/agents', methods=['POST'])
def create_agent():
data = request.json
required_fields = ['name', 'traits']
for field in required_fields:
if field not in data:
return handle_error(f"Missing required field: {field}")
try:
new_agent = Agent(
name=data['name'],
description=data.get('description', ''),
traits=data['traits'],
image=data.get('image'),
skill=data.get('skill'),
communication_style=data.get('communication_style'),
learning_style=data.get('learning_style'),
response_length=data.get('response_length')
)
db.session.add(new_agent)
db.session.commit()
return jsonify(new_agent.to_dict()), 201
except Exception as e:
db.session.rollback()
return handle_error(f"Error creating agent: {str(e)}")
The Agent model defines the structure and attributes of AI agents stored in the PostgreSQL database.
class Agent(db.Model):
__tablename__ = 'agents'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), nullable=False)
description = db.Column(db.Text, nullable=True)
traits = db.Column(ARRAY(db.String), nullable=False, default=[])
image = db.Column(db.String(500), nullable=True)
skill = db.Column(db.String(255), nullable=True)
communication_style = db.Column(db.String(255), nullable=True)
learning_style = db.Column(db.String(255), nullable=True)
response_length = db.Column(db.String(50), nullable=True)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
def to_dict(self):
return {
"id": self.id,
"name": self.name,
"description": self.description,
"traits": self.traits,
"image": self.image,
"skill": self.skill,
"communication_style": self.communication_style,
"learning_style": self.learning_style,
"response_length": self.response_length,
"created_at": self.created_at,
"updated_at": self.updated_at
}
Oracle includes a CLI tool to initialize the database effortlessly.
@app.cli.command("init-db")
def init_db():
"""Initialize the database."""
db.create_all()
print("Database initialized successfully!")