banner
Vinking

Vinking

愿星光永远偏爱你的梦境

Fun to play! Connect the article page to the large model.

Recently, I integrated TianliGPT into the article, achieving an automatic article summary (TL;DR) module. TianliGPT provides a very simple and quick embedding method, but it focuses on summary generation and related article recommendation features. If you want to expand its capabilities, there are significant limitations. Therefore, I recently abandoned TianliGPT in favor of using Moonshot AI for article summary generation and additional feature expansion.

Determine Requirements#

First, we want to not only generate summaries for articles but also pose relevant questions about the content of the articles along with corresponding answers. Clicking on a question will display the answer to that question. The effect is similar to the image below:

Article Summary Module Preview

Based on the above requirements, we need the model to return content in a JSON format similar to the one below, which will then be processed by the frontend:

We can then design the following prompt to give to the model:

Note

A prompt refers to the information or instructions provided to the model to guide it in generating specific responses or outputs. It determines how the model understands and processes user input. An effective prompt typically includes the following elements: clarity, relevance, conciseness, context, and directive.

Since Kimi and the model provided by Moonshot AI are from the same source, we can use Kimi for testing to predict the results we might obtain when using the Moonshot API to some extent (actually to save money). To verify whether this prompt can achieve the desired effect, I tried using it in a conversation with Kimi, and the results are shown in the image below:

Model Conversation Result

Initiating a Conversation with the Model#

After resolving what to communicate with the model, we need to address how to communicate with the model. The official documentation of Moonshot AI provides implementation methods in both Python and Node.js, while here we will use PHP to implement the corresponding functionality.

The official API provided for us is Chat Completions: https://api.moonshot.cn/v1/chat/completions, and examples of the request headers and request content are as follows:

  • model is the model name; Moonshot-v1 currently has three models: moonshot-v1-8k, moonshot-v1-32k, and moonshot-v1-128k.
  • The messages array is a list of conversation messages, where the role can be system, user, or assistant: system represents system messages that provide context or guidance for the conversation, usually filled with the prompt; user represents the user's message, i.e., the user's question or input; assistant represents the model's reply.
  • temperature is the sampling temperature, recommended to be 0.3.

We can construct a MoonshotAPI class to implement this functionality:

Note

If you directly tell Kimi the large model in the prompt: "Please output content in JSON format," Kimi can understand your request and will generate a JSON document as required, but the generated content usually has some flaws, such as outputting additional text outside the JSON document to explain it.

So here we need to enable JSON Mode when constructing the request content, allowing the model to "output a valid, correctly parsable JSON document as requested," which means adding 'response_format' => ["type" => "json_object"] to the return array of the preparePayload method.


It is evident that among the three parameters accepted by the MoonshotAPI class, only the $messages conversation message list is relatively complex. Therefore, we create a getMessages function to construct the conversation message list array.

Here we fill in the initially designed prompt into the system message and the article content into the first user message. In the actual API request, the messages array will be arranged in chronological order, usually starting with the system message, followed by the user question, and finally the assistant reply. This structure helps maintain the context and coherence of the conversation.


When we communicate with the model, it will return JSON data like this, where we mainly focus on the choices array.

In our conversation mode, the choices array usually contains only one object (which is why we write $result['choices'][0] when retrieving the model's reply and other information), and this object represents the text reply generated by the model. The finish_reason within the object indicates the reason for the completion of the generated text; if the model believes it has provided a complete answer, the value of finish_reason will be stop. Therefore, we can use this to determine whether the content generated by the model is complete. The content within the object is the reply given to us by the model.

Next, we create the Moonshot function to call the MoonshotAPI class:

Now we have the code as shown below. If there are no unexpected issues, calling it directly will yield the model's reply✌️. The frontend can then render the reply on the page, which will not be elaborated further here.

Appendix: Handling Long Articles#

For some articles, directly calling the above code may result in the following error:

The reason for this error is that the total number of input and output context tokens exceeds the model's (here we assume we are using the moonshot-v1-8k model) set token limit. We need to choose an appropriate model based on the length of the context, plus the expected output token length. For this situation, the documentation also provides an example code on how to choose the appropriate model based on context length, and we just need to convert the code to PHP and integrate it into our previous code.

In the Moonshot function, when the model returns an error of type invalid_request_error (i.e., exceeding the model's maximum token limit), we call the selectModel function to choose the most suitable model and then re-engage in the conversation with the appropriate model.

This article is updated by Mix Space to xLog. The original link is https://www.vinking.top/posts/codes/developing-auto-summary-module-using-ai

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.