# Using AI To Tag And Categorize Forum Posts

**URL:** https://meta.discourse.org/t/using-ai-to-tag-and-categorize-forum-posts/356711
**Category:** Support
**Tags:** ai
**Created:** [March 11, 2025, 12:29am UTC](https://meta.discourse.org/t/using-ai-to-tag-and-categorize-forum-posts/356711 "2025-03-11T00:29:14Z")
**Posts on this page:** 1
**Showing post:** 5

<div class="post-metadata">

### Author: ![BrianC](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/brianc/32/487568_2.png) [@BrianC](https://meta.discourse.org/u/BrianC)
#### Post date: [March 11, 2025, 7:43pm UTC](https://meta.discourse.org/t/using-ai-to-tag-and-categorize-forum-posts/356711/5 "2025-03-11T19:43:26Z")

</div>

Thanks Sam,

> [@sam](#):
>
> Curious to see how this approach works for you, you will need to test the tool, it should be quite straightforward from the tool UI.

Easier for some than other. I am in the latter category 😂

I am fiddling around with this and haven’t got this to work. I have no tool experience so actually looking forward to getting this working to think about other ways to use tools.

I tried using the code from your blog with an API key which did not work. AI suggested I use a hard coded url so I tried that without success.

The API Key was not called and there are no logs that were created with errors.

> This is used to automatically categorize topics on creation
> 
> I will classify the topic “Insights from Jesus’ early years and miracles” into the category of **New Testament** , as it relates to the life and teachings of Jesus.  
> Moving the topic now…

This is what I tried.

> /\*\*
> 
> - Tool API Quick Reference
> - 
> - Entry Functions
> - 
> - invoke(parameters): Main function. Receives parameters (Object). Must return a JSON-serializable value.
> - Example:
> - function invoke(parameters) { return “result”; }
> - 
> - details(): Optional. Returns a string describing the tool.
> - Example:
> - function details() { return “Tool description.”; }
> - 
> - Provided Objects
> - 
> - 
> 1. http
> 
> - http.get(url, options?): Performs an HTTP GET request.
> - Parameters:
> - 
> 
> ```
> url (string): The request URL.
> 
> ```
> 
> - 
> 
> ```
> options (Object, optional):
> 
> ```
> 
> - 
> 
> ```
> headers (Object): Request headers.
> 
> ```
> 
> - Returns:
> - 
> 
> ```
> { status: number, body: string }
> 
> ```
> 
> - 
> - http.post(url, options?): Performs an HTTP POST request.
> - Parameters:
> - 
> 
> ```
> url (string): The request URL.
> 
> ```
> 
> - 
> 
> ```
> options (Object, optional):
> 
> ```
> 
> - 
> 
> ```
> headers (Object): Request headers.
> 
> ```
> 
> - 
> 
> ```
> body (string): Request body.
> 
> ```
> 
> - Returns:
> - 
> 
> ```
> { status: number, body: string }
> 
> ```
> 
> - 
> - (also available: http.put, http.patch, http.delete)
> - 
> - Note: Max 20 HTTP requests per execution.
> - 
> - 
> 1. llm
> 
> - llm.truncate(text, length): Truncates text to a specified token length.
> - Parameters:
> - 
> 
> ```
> text (string): Text to truncate.
> 
> ```
> 
> - 
> 
> ```
> length (number): Max tokens.
> 
> ```
> 
> - Returns:
> - 
> 
> ```
> Truncated string.
> 
> ```
> 
> - 
> - 
> 1. index
> 
> - index.search(query, options?): Searches indexed documents.
> - Parameters:
> - 
> 
> ```
> query (string): Search query.
> 
> ```
> 
> - 
> 
> ```
> options (Object, optional):
> 
> ```
> 
> - 
> 
> ```
> filenames (Array): Limit search to specific files.
> 
> ```
> 
> - 
> 
> ```
> limit (number): Max fragments (up to 200).
> 
> ```
> 
> - Returns:
> - 
> 
> ```
> Array of { fragment: string, metadata: string }
> 
> ```
> 
> - 
> - 
> 1. upload
> 
> - upload.create(filename, base\_64\_content): Uploads a file.
> - Parameters:
> - 
> 
> ```
> filename (string): Name of the file.
> 
> ```
> 
> - 
> 
> ```
> base_64_content (string): Base64 encoded file content.
> 
> ```
> 
> - Returns:
> - 
> 
> ```
> { id: number, short_url: string }
> 
> ```
> 
> - 
> - 
> 1. chain
> 
> - chain.setCustomRaw(raw): Sets the body of the post and exist chain.
> - Parameters:
> - 
> 
> ```
> raw (string): raw content to add to post.
> 
> ```
> 
> - 
> - Constraints
> - 
> - Execution Time: ≤ 2000ms
> - Memory: ≤ 10MB
> - HTTP Requests: ≤ 20 per execution
> - Exceeding limits will result in errors or termination.
> - 
> - Security
> - 
> - Sandboxed Environment: No access to system or global objects.
> - No File System Access: Cannot read or write files.  
> \*/
> 
> /\*\*
> 
> - Discourse Topic Categorizer
> - 
> - This tool allows you to change the category of a Discourse topic
> - using the Discourse API.  
> \*/
> 
> /\*\*
> 
> - Discourse Topic Categorizer
> - 
> - This tool allows you to change the category of a Discourse topic
> - using the Discourse API.  
> \*/
> 
> /\*\*
> 
> - Discourse Topic Categorizer
> - 
> - This tool allows you to change the category of a Discourse topic
> - using the Discourse API.  
> \*/
> 
> function invoke(params) {  
> // Required parameters validation  
> if (!params.topic\_id) {  
> return { error: “Missing required parameter: topic\_id” };  
> }
> 
> if (!params.category\_id) {  
> return { error: “Missing required parameter: category\_id” };  
> }
> 
> // Base URL for your Discourse instance  
> const baseUrl = “[https://community.mysite.com](https://community.mysite.com)”;
> 
> // Full API endpoint URL for updating a topic  
> const apiUrl = `${baseUrl}/t/${params.topic_id}.json`;
> 
> // Prepare request body  
> const requestBody = {  
> category\_id: params.category\_id  
> };
> 
> // Optional parameter: update the title if provided  
> if (params.title) {  
> requestBody.title = params.title;  
> }
> 
> // Use your provided API key  
> const apiKey = “Discourse-API-Key”;
> 
> try {  
> // Make PUT request to update the topic  
> const response = http.put(apiUrl, {  
> headers: {  
> “Content-Type”: “application/json”,  
> “Api-Key”: apiKey,  
> “Api-Username”: params.api\_username || “system”  
> },  
> body: JSON.stringify(requestBody)  
> });
> 
> ```
> if (response.status >= 200 && response.status < 300) {
> return {
> success: true,
> topic_id: params.topic_id,
> category_id: params.category_id,
> response: JSON.parse(response.body)
> };
> } else {
> return {
> error: `Failed to update topic category. Status: ${response.status}`,
> details: response.body
> };
> }
> 
> ```
> 
> } catch (error) {  
> return {  
> error: “An error occurred while updating the topic category”,  
> details: error.toString()  
> };  
> }  
> }
> 
> function details() {  
> return “Categorizes a topic by moving it to a specified category”;  
> }

---

_[View the full topic](https://meta.discourse.org/t/using-ai-to-tag-and-categorize-forum-posts/356711)._
