## 6 Model Application and Deployment
### Testing Model Performance and Optimization Strategies  
The cosine similarity coefficient is a measure of the angle between two vectors, particularly useful in the field of text analysis for assessing the similarity between different texts. It reflects the consistency of their directions in high-dimensional space by calculating the cosine of the angle between the two vectors. This measure is particularly effective in text corpora, as text can often be represented as high-dimensional vectors, with each dimension corresponding to the frequency of occurrence of a word or phrase. To evaluate the generative effectiveness and refine output quality, a series of targeted testing strategies were employed. Central to this effort was the development of a function named generate_text_with_circuit_breaker, which integrates an intelligent circuit-breaking mechanism to govern the coherence and diversity of the generated text. The function operates through the following steps: \begin{table}
\begin{tabular}{c c c c c} \hline \hline
**Metric Names** & **Loss** & **Accuracy** & **Recall** & **F1 Score** \\ \hline Result & 0.066 & 0.986 & 0.986 & 0.986 \\ \hline \hline \end{tabular}
\end{table} 
Table 6: Evaluation metrics results for a Transformer model trained with a mixture of Chinese and Tibetan languages. Figure 15: Loss function curve and accuracy curve graph for a Transformer model trained with a mixture of Chinese and Tibetan languages. * Sentence Integrity Check: Ensures each candidate output concludes with punctuation such as periods, question marks, or exclamation points, preserving natural sentence integrity. * Cosine Similarity Evaluation: Evaluate the similarity between newly generated sentences and previous text using TF-IDF vectorization and cosine similarity calculation. When the similarity is too high (exceeding a preset threshold, such as 0.8), trigger a fuse mechanism to terminate further text generation, thus preventing content disassociation and redundancy. * Filtering Out Sentences Ending with Question Marks: Exclude sentences ending with question marks from the final output to better suit the needs of certain scenarios, such as declarative answers. The generate_text_with_circuit_breaker function embodies a circuit-breaking strategy designed to excise segments from the generated text that closely resemble the input text, thus guaranteeing freshness and variety in the output. By assessing paragraph-to-paragraph cosine similarities and retaining segments only when their similarity falls below a set threshold and concludes with specific characters (like periods or exclamation marks), the function averts the repetition of information, enhancing response quality. Initially, the calculate_similarity function is defined, utilizing TfidfVectorizer and cosine_similarity to quantify the cosine similarity between two pieces of text. This step translates text into TF-IDF vector representations, subsequently measuring their semantic similarity through cosine similarity. Lower similarity scores indicate greater content disparity between the texts. In practical experiments, the application of cosine similarity is significant. The text generated by the model's inference is a continuous sequence, and its length is controlled by parameters such as d_model and max_new_tokens. d_model represents the word vector dimension and determines the demand for computational resources to a certain extent. max_new_tokens is a threshold that determines the number of consecutive tokens generated from the model's inference, which produces a series of coherent and contextually related tokens. However, in practice, our training parameters are relatively small, and generating longer texts can be understood as extracting corresponding text blocks from the corpus. Ensuring the integrity of the output result cannot be guaranteed if word-based context fusion is interrupted. Therefore, in the experiment, the generated text undergoes a similarity check by segmenting it into paragraphs.