Building AI Agents: From Basics to OpenAI-Compatible LLM Calls
This article explains the fundamental concepts of AI agents, their perception‑reasoning‑action loop, the evolution from rule‑based bots to LLM‑driven agents, and provides step‑by‑step Python and PHP code for invoking a large language model via the OpenAI‑compatible API.
Overview
Agents connect large language models (LLMs) to real‑world scenarios via a perception‑reasoning‑action loop.
Definition of an Agent
An agent is an entity that perceives its environment through sensors, reasons autonomously, and acts via actuators to achieve goals.
Sensors : acquire external information (e.g., text input, camera images).
Reasoning : analyzes perception and makes decisions.
Actuators : execute decisions (e.g., generate reply text, move a robot).
Evolution of Agents
1. Fixed‑pattern replies
Hard‑coded input‑output mappings with no scalability.
2. Random string generation
Outputs random characters without regard to intent.
3. Rule‑based pattern matching
Pre‑defined rules perform text substitution; rule sets grow exponentially, causing maintenance and performance issues.
LLMs provide powerful natural‑language understanding and generation, solving the reasoning bottleneck.
LLM as the Agent’s Brain
LLMs are treated as a black‑box that receives a string and returns a string. The core interaction follows the OpenAI‑compatible API specification.
Calling a Large Model with the OpenAI‑compatible API
Preparation
Obtain an API key from Alibaba Cloud Baichuan console: https://bailian.console.aliyun.com
Python example
import os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen-plus",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "你是谁?"}
]
)
print(completion.choices[0].message.content)PHP example
<?php
$url = 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions';
$apiKey = getenv('DASHSCOPE_API_KEY');
$headers = [
'Authorization: Bearer '.$apiKey,
'Content-Type: application/json'
];
$data = [
"model" => "qwen-plus",
"messages" => [
["role" => "system", "content" => "You are a helpful assistant."],
["role" => "user", "content" => "你是谁?"]
]
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
echo $response;Key points
Client initialization: set base_url to the model endpoint and provide api_key for authentication.
The messages parameter is an array of dictionaries, each with a role (e.g., system, user) and content. The system role defines model behavior; the user role supplies the query.
Sample output
{
"model": "qwen-plus",
"id": "chatcmpl-9d0286c3-5c86-9a50-a41c-8271e6d42d43",
"choices": [
{
"message": {
"content": "你好!我是通义千问(Qwen),阿里巴巴集团旗下的超大规模语言模型。我能够回答问题、创作文字、逻辑推理、编程等。",
"role": "assistant"
},
"index": 0,
"finish_reason": "stop"
}
]
}Summary
Agents rely on the perception‑reasoning‑action loop; autonomous reasoning is essential.
LLMs serve as the “brain,” a black‑box mapping string inputs to string outputs.
OpenAI‑compatible specifications enable interchangeable model calls across vendors.
The messages parameter orchestrates system prompts and user queries to convey intent precisely.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
