Backend Development 9 min read

Intelligent Backend Menu Search with OpenAI Embeddings, LangChain, and DIFY

The article demonstrates how to improve backend menu navigation by building a knowledge base of menu metadata, generating concise Chinese descriptions with OpenAI embeddings, and implementing RAG retrieval using both LangChain code orchestration and DIFY’s visual workflow, highlighting each approach’s flexibility and ease of use.

37 Interactive Technology Team
37 Interactive Technology Team
37 Interactive Technology Team
Intelligent Backend Menu Search with OpenAI Embeddings, LangChain, and DIFY

Background: As the business expands, the backend system menu items increase, causing difficulty for users to locate specific functions. Menu names across sub-modules may be ambiguous.

Considering the popularity of OpenAI, Text‑Embedding can be used for intelligent retrieval to improve user experience.

This article introduces two solutions for search practice: (1) Using LangChain to implement RAG retrieval via code orchestration; (2) Using DIFY workflow for visual configuration of RAG retrieval.

Embedding Model and Knowledge Base: The effectiveness of retrieval depends on the quality of the embedding model and the content of the knowledge base. The chosen model is OpenAI's text-embedding-3-large . The knowledge base consists of exported frontend menu route data, covering fields such as menu name, parent directory, full pinyin, abbreviation, path, and description.

Knowledge base dimensions are defined in a table (menu name, parent directory, full pinyin, abbreviation, path, description).

Knowledge base organization: (1) Export menu name, parent directory, and path via API; (2) Generate full and abbreviated pinyin using a third‑party library; (3) Generate menu descriptions using AI (LangChain + GPT‑3.5).

const prompt = PromptTemplate.fromTemplate(`
# 角色
你是一位后台管理菜单的详细描述专家。你的主要职责是根据给定的父级目录名称以及菜单名称,生成简洁且准确的后台菜单描述。

## 技能
### 技能1:生成后台菜单的描述信息
- 根据提供的父级目录:<{parent_menu}> 和菜单名称:<{menu}>,为该后台菜单构建一段不超过40个字的描述信息。
- 在描述信息中,应包括该菜单所能执行的主要操作。

## 限制
- 你返回的信息不能包含任何换行或者换行符。
- 父级目录和菜单名称在<>中。
- 描述信息必须用中文回答并且不超过40个字。
- 只处理与后台菜单描述信息相关的问题。如果用户询问了其他问题,不要回答。
- 对于未指定的信息,根据菜单名称推测其功能。
{format_instructions}
`);

After running LangChain, the generated descriptions roughly meet expectations.

Implementation with LangChain: The workflow consists of four steps – prompt engineering, query‑chain construction, knowledge‑base embedding storage (e.g., Pinecone), and RAG retrieval with top‑K results.

// 2.1 Prompt
const task = `
  {format_instructions}
  Given a query, Expand the processed words by transforming synonyms or translating to grasp the user's intent more precisely.Answer in Chinese.
  Return the Array, the length of the array should be less than 2.
  This is the query: {query}
  Answer:`;
const parser = StructuredOutputParser.fromZodSchema(
  z.object({
    menuArray: z.array(z.string()).describe(''),
  }),
);
const chain = RunnableSequence.from([
  new PromptTemplate({
    template: task,
    inputVariables: ['query'],
    partialVariables: { format_instructions: parser.getFormatInstructions() },
  }),
  new OpenAI({
     azureOpenAIApiKey: AZURE_API_KEY,
     azureOpenAIApiInstanceName: AZURE_INSTANCE_NAME,
     azureOpenAIApiDeploymentName: AZURE_DEPLOYMENT_16K_NAME,
     azureOpenAIApiVersion: AZURE_VERSION,
     temperature: 0,
     modelName: 'gpt-4',
     maxTokens: 4096,
  }),
  parser,
]);

Using DIFY: DIFY provides a visual workflow to configure the same RAG pipeline without writing code. The knowledge base is built from an Excel file, supporting upload, text segmentation, token‑cost estimation, and CRUD operations.

Workflow configuration includes visual prompt templates, conditional branches, LLM, and RAG chain, with special handling of output parsing.

Debugging and logging are simplified with preview, run logs, and data‑overview panels.

Conclusion: LangChain offers flexibility for custom development, while DIFY delivers a more out‑of‑the‑box solution for scenarios that do not require deep customization.

LangChainRAGknowledge baseBackend SearchDIFYOpenAI Embedding
37 Interactive Technology Team
Written by

37 Interactive Technology Team

37 Interactive Technology Center

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.