How bianbianzhu/openai-function-calling works
Project type and purpose
• This is a Node.js/TypeScript backend “agent” application that uses OpenAI’s Function Calling API to provide a chat-style interface for exploring and booking farm activities.
• There is no browser-based UI—users drive it via a CLI or embedding in another service.
Top-level view of the file structure
• src/index.ts – the application entry point, orchestrates user input, chat flow, and output
• src/scripts/generate-function-files.js – generates TypeScript function stubs from the function-description metadata
• src/services – “service layer” containing:
– openai.ts: wrapper around OpenAI API calls
– chat.ts: coordinates chat completion requests, handles function call responses
– prompts.ts & user-prompt-interface.ts: build and normalize messages/prompts
– function-descriptions*.ts: metadata definitions for each callable function
– functions/*: actual implementations of each function (get_farms, book_activity, etc.)
• src/utils – shared helper modules and tests (chat-utils, common-utils, type-utils)
• Config files – package.json, tsconfig.json, jest.config.ts
Main components
a. User Interface (CLI or calling code)
b. Application Orchestrator (src/index.ts)
c. Chat Service (src/services/chat.ts)
d. OpenAI API Client (src/services/openai.ts)
e. Function Definition Registry (function-descriptions*.ts)
f. Function Executors (src/services/functions/*)
g. Utility Library (src/utils)
h. Code-gen Script (src/scripts/generate-function-files.js)
Relationships and data flow
1. User sends input → index.ts
2. index.ts invokes Chat Service → chat.ts
3. Chat Service constructs a messages array via Prompt & User-Prompt modules
4. Chat Service calls OpenAI API Client → openai.ts
5. OpenAI returns either a normal chat response or a Function Call directive
6. Chat Service routes function calls to the appropriate Function Executor
7. Function Executor runs business logic (e.g., fetch farms, book activity) → returns result
8. Chat Service packages the function result into a follow-up chat message → returns to index.ts → output to user
Architectural patterns and principles
• Layered separation (orchestrator → service → domain/function → utils)
• Single responsibility for each module (each function file implements exactly one API function)
• Metadata-driven generation: function definitions drive both chat prompts and stub generation
• Open/Closed for new functions–you add enums, descriptions, stubs, and handlers without touching core chat flow