Skip to main content
Glama
llms.txt9.42 kB
# Xiaohongshu (XHS) API Integration with Large Language Models (LLMs) This document explains how to integrate the Xiaohongshu API microservice with Large Language Models (such as GPT, Claude, etc.) to enable powerful content analysis and generation capabilities. ## Basic Integration Architecture ### 1. Data Flow ``` Xiaohongshu API Microservice -> Data Processing Layer -> LLM API ``` ### 2. Common Use Cases - Note content analysis and summarization - User interest profile generation - Trending topic discovery and analysis - Content creation assistance and recommendations - Comment sentiment analysis ## API Endpoints Reference Before diving into LLM integration, here's a reference of the key API endpoints available in the Xiaohongshu MCP Server: ### Client Management - `POST /clients` - Create a new XHS client instance - Input: `{"cookie": "your_cookie", "user_agent": "optional_user_agent", "timeout": 10}` - Output: `{"client_id": "client_1"}` - `GET /clients` - List all client IDs - Output: `["client_1", "client_2", ...]` - `DELETE /clients/{client_id}` - Delete a client instance - Output: `{"status": "success"}` ### Note Operations - `POST /clients/{client_id}/note` - Get note by ID - Input: `{"note_id": "note_id", "xsec_token": "token", "xsec_source": "pc_feed"}` - Output: Complete note data including title, content, images, etc. - `POST /clients/{client_id}/note/html` - Get note by ID from HTML - Input: Same as above - Output: Note data extracted from HTML ### Search Operations - `POST /clients/{client_id}/search/notes` - Search notes by keyword - Input: `{"keyword": "search_term", "page": 1, "page_size": 20, "sort": "general", "note_type": 0}` - Output: List of matching notes - `POST /clients/{client_id}/search/users` - Search users by keyword - Input: `{"keyword": "username"}` - Output: List of matching users ### User Operations - `POST /clients/{client_id}/user/info` - Get user info by ID - Input: `{"user_id": "user_id"}` - Output: User profile information - `POST /clients/{client_id}/user/notes` - Get user notes - Input: `{"user_id": "user_id"}` - Output: List of notes by the user ### Feed Operations - `GET /clients/{client_id}/feed/categories` - Get feed categories - Output: List of available feed categories - `GET /clients/{client_id}/feed/{feed_type}` - Get feed by type - Output: Feed content for the specified type ## Integration with OpenAI API Here's a Python example showing how to send Xiaohongshu note content to OpenAI API for analysis: ```python import requests import json import openai # Configure OpenAI API openai.api_key = "your_openai_api_key" # Xiaohongshu API microservice base URL XHS_API_BASE = "http://localhost:8000" # Step 1: Create Xiaohongshu client def create_xhs_client(cookie): response = requests.post( f"{XHS_API_BASE}/clients", json={"cookie": cookie} ) return response.json()["client_id"] # Step 2: Get note content def get_note(client_id, note_id, xsec_token): response = requests.post( f"{XHS_API_BASE}/clients/{client_id}/note", json={"note_id": note_id, "xsec_token": xsec_token} ) return response.json() # Step 3: Analyze note content with OpenAI def analyze_note_with_openai(note_data): # Extract note title and content title = note_data.get("title", "") desc = note_data.get("desc", "") # Construct prompt prompt = f""" Analyze the following Xiaohongshu (RED) note content: Title: {title} Content: {desc} Please provide the following analysis: 1. Main topics and keywords 2. Sentiment (positive/negative/neutral) 3. Content quality assessment 4. Potential target audience 5. Content improvement suggestions """ # Call OpenAI API response = openai.ChatCompletion.create( model="gpt-4", messages=[ {"role": "system", "content": "You are a professional content analyst specializing in social media content."}, {"role": "user", "content": prompt} ] ) return response.choices[0].message.content # Main process def main(): # Configuration cookie = "your_xhs_cookie" note_id = "target_note_id" xsec_token = "your_xsec_token" # Execute workflow client_id = create_xhs_client(cookie) note_data = get_note(client_id, note_id, xsec_token) analysis = analyze_note_with_openai(note_data) print("Note Analysis Results:") print(analysis) if __name__ == "__main__": main() ``` ## Integration with Claude API Here's an example of using Anthropic's Claude API for similar analysis: ```python import requests import json import anthropic # Configure Claude API claude_client = anthropic.Anthropic(api_key="your_anthropic_api_key") # Xiaohongshu API microservice base URL XHS_API_BASE = "http://localhost:8000" # Step 1 and Step 2 are the same as in the OpenAI example # Step 3: Analyze note content with Claude def analyze_note_with_claude(note_data): # Extract note title and content title = note_data.get("title", "") desc = note_data.get("desc", "") # Construct prompt prompt = f""" Analyze the following Xiaohongshu (RED) note content: Title: {title} Content: {desc} Please provide the following analysis: 1. Main topics and keywords 2. Sentiment (positive/negative/neutral) 3. Content quality assessment 4. Potential target audience 5. Content improvement suggestions """ # Call Claude API response = claude_client.messages.create( model="claude-3-opus-20240229", max_tokens=1000, messages=[ {"role": "user", "content": prompt} ] ) return response.content[0].text # Main process is similar to the OpenAI example ``` ## Batch Processing and Data Analysis For scenarios requiring batch processing of multiple notes, you can use the following workflow: 1. Use the Xiaohongshu API to retrieve multiple notes (e.g., through search or user note lists) 2. Store the data in a temporary database or file 3. Send batches to the LLM for analysis 4. Aggregate the analysis results Example code framework: ```python # Get all notes from a user def get_all_user_notes(client_id, user_id): response = requests.post( f"{XHS_API_BASE}/clients/{client_id}/user/notes", json={"user_id": user_id} ) return response.json() # Batch analyze notes def batch_analyze_notes(client_id, notes_data, llm_type="openai"): results = [] for note in notes_data.get("notes", []): note_id = note.get("note_id") xsec_token = note.get("xsec_token", "") # Get complete note content full_note = get_note(client_id, note_id, xsec_token) # Analyze based on selected LLM if llm_type == "openai": analysis = analyze_note_with_openai(full_note) elif llm_type == "claude": analysis = analyze_note_with_claude(full_note) results.append({ "note_id": note_id, "analysis": analysis }) return results ``` ## Important Considerations 1. **API Limits**: Be mindful of LLM API rate limits and costs, especially when batch processing. 2. **Data Privacy**: Ensure sensitive personal information is removed before sending data to LLMs. 3. **Error Handling**: Implement proper error handling and retry mechanisms in production applications. 4. **Content Compliance**: Ensure your processing and analysis methods comply with relevant regulations and platform policies. 5. **Prompt Engineering**: Optimize prompts based on specific needs to get more accurate analysis results. ## Advanced Applications 1. **Content Clustering**: Use LLMs to cluster large volumes of notes by topic 2. **Trend Prediction**: Analyze historical data to predict future content trends 3. **Personalized Recommendations**: Combine user behavior history with content analysis for personalized recommendations 4. **Multimodal Analysis**: Combine image and text analysis for comprehensive insights (requires multimodal-capable LLMs) ## Example Use Cases with API Combinations ### Content Trend Analysis ```python # 1. Search for notes on a specific topic search_results = search_notes(client_id, "skincare routine") # 2. Analyze each note with LLM trend_data = batch_analyze_notes(client_id, search_results) # 3. Extract patterns and emerging trends ``` ### User Interest Profiling ```python # 1. Get user's notes user_notes = get_all_user_notes(client_id, user_id) # 2. Get user's liked notes user_likes = get_user_likes(client_id, user_id) # 3. Analyze both sets with LLM to create interest profile ``` ### Content Creation Assistant ```python # 1. Search for top-performing notes in a category top_notes = search_notes(client_id, "travel tips", sort="popularity_descending") # 2. Analyze with LLM to extract successful patterns # 3. Generate content suggestions based on patterns ``` ## Resources and References - [OpenAI API Documentation](https://platform.openai.com/docs/api-reference) - [Anthropic Claude API Documentation](https://docs.anthropic.com/claude/reference/getting-started-with-the-api) - [LangChain Framework](https://python.langchain.com/docs/get_started/introduction) - Framework for building LLM applications - [Xiaohongshu API Microservice Documentation](/docs) - Local service API documentation

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/proerror77/xhs_mcp_agent'

If you have feedback or need assistance with the MCP directory API, please join our Discord server