Hand‑crafting a TypeScript Interface to Connect with an AI Large Model for Code Review
This tutorial walks through setting up a Node.js environment, creating a TypeScript project, writing a custom OpenAI client, and integrating either Zhipu AI or an Ollama‑deployed model to perform automated code reviews via GitLab CI, complete with build, run, and VS Code debugging steps.
Environment Installation
Install Node.js and NVM (Node Version Manager) to manage Node versions. The article links to two external guides for NVM installation.
Project Setup
Initialize an empty directory and run npm init -y to create package.json. Install TypeScript and ts-node globally with npm install -g typescript ts-node -g. Edit package.json to add scripts, bin entry, and dependencies such as axios, commander, and type definitions.
{
"name": "haizei-ai-codereview-gitlab",
"version": "1.0.0",
"main": "index.js",
"bin": { "ai-code-reviewer": "./bin/index.js" },
"scripts": { "start": "tsc --watch", "build": "tsc" },
"files": ["lib", "bin"],
"license": "MIT",
"dependencies": {
"@types/axios": "^0.14.0",
"@types/commander": "^2.12.2",
"@types/node": "^20.2.1",
"axios": "^1.4.0",
"commander": "^10.0.1",
"typescript": "^5.5.4"
}
}Create tsconfig.json with strict TypeScript options, targeting es2016 and using commonjs modules. Important flags include strict, sourceMap, and outDir": "lib".
Writing the OpenAI Client (openai.ts)
Import axios and define an ICompletion interface that mirrors the request payload (messages, temperature, model). Create a constant zhipuAIRequestBody with temperature: 0, model: "glm-4", and stream: false.
import axios, { AxiosInstance } from 'axios';
interface ICompletion {
messages?: { role: string, content: string }[];
temperature: number;
model: string;
}
export const zhipuAIRequestBody = {
"temperature": 0,
"model": "glm-4",
"stream": false
};
export class OpenAI {
private apiClient: AxiosInstance;
private accessTokens: string;
constructor(private apiUrl: string, private accessToken: string) {
this.accessTokens = accessToken;
this.apiClient = axios.create({ baseURL: apiUrl });
}
async executeCodeReview(change: string): Promise<string> {
const data: ICompletion = zhipuAIRequestBody;
data.messages = [
{ role: 'system', content: '你是一个AI代码评审专家,请帮我评审这个代码。' },
{ role: 'user', content: change }
];
const response = await this.apiClient.post('/chat/completions', data, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.accessTokens}`
}
});
return response.data.choices?.[0]?.message?.content;
}
}Using the Client in index.ts
Import the OpenAI class, instantiate it with the API URL and token, and call executeCodeReview with a code snippet. Log the returned suggestion.
// 引入一个AI模块
import { OpenAI } from './openai';
async function executeStart() {
console.log("AI大模型代码评审程序开始执行");
const openai = new OpenAI("https://open.bigmodel.cn/api/paas/v4", "xxxxxxxKey");
const suggestion = await openai.executeCodeReview("System.out.println(5/0)");
console.log(suggestion);
}
module.exports = executeStart;Compile the project with npm run build (which runs tsc) and then execute the generated JavaScript via the binary script:
node bin/index.jsOllama Private Deployment Alternative
If Zhipu AI is unavailable, deploy the qwen2:1.5b model with Ollama, adjust the API URL in index.ts, rebuild, and run the same command to obtain review results.
VS Code IDE Integration
Install Visual Studio Code, open the project folder, and optionally install the ts-node runner extension. With the extension, you can run the script directly from the IDE without a separate compile step, and use the debugger to step through the code.
Summary of the Process
The article demonstrates a complete workflow: environment preparation, TypeScript project scaffolding, custom OpenAI client implementation, execution via compiled binaries, and optional private model deployment, providing a hands‑on example of building an AI‑powered code‑review tool.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ubiquitous Tech
A ubiquitous public account for pirate enthusiasts, regularly sharing curated experiences, tech learning, and growth insights. Currently publishing articles on AI RAG customer service, AI MCP technology, and open-source design. Personal free Knowledge Planet: Awakening New World Programmer.
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.
