Spring AI教程(十四):理解嵌入技术与Token管理

在前面的文章中,我们探讨了Prompt模板和嵌入技术的应用。这篇文章将进一步解释嵌入技术的实际应用场景,并探讨Token的管理及其在AI模型中的重要性。

嵌入技术(Embeddings)

嵌入技术将文本转换为数值数组或向量,使AI模型能够处理和解释语言数据。这种从文本到数值再到文本的转换,是AI与人类语言互动和理解的关键元素。

嵌入技术的实际应用

嵌入在实际应用中非常重要,例如在检索增强生成(RAG)模式中。它们将数据表示为语义空间中的点,这类似于欧几里德几何中的二维空间,但在更高的维度中。文本中相似主题的句子在这个多维空间中位置更近,这有助于文本分类、语义搜索甚至产品推荐等任务。

嵌入示例

以下是一个使用嵌入技术进行文本相似性计算的示例:

import org.springframework.stereotype.Service;
import com.example.springai.OpenAiEmbeddingService;
import java.util.List;

@Service
public class EmbeddingService {

    private final OpenAiEmbeddingService embeddingService;

    public EmbeddingService(OpenAiEmbeddingService embeddingService) {
        this.embeddingService = embeddingService;
    }

    public List<Float> generateEmbedding(String text) {
        return embeddingService.embed(text);
    }
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class SimilarityController {

    @Autowired
    private EmbeddingService embeddingService;

    @GetMapping("/calculate-similarity")
    public double calculateSimilarity(@RequestParam String text1, @RequestParam String text2) {
        List<Float> embedding1 = embeddingService.generateEmbedding(text1);
        List<Float> embedding2 = embeddingService.generateEmbedding(text2);
        return cosineSimilarity(embedding1, embedding2);
    }

    private double cosineSimilarity(List<Float> vectorA, List<Float> vectorB) {
        double dotProduct = 0.0;
        double normA = 0.0;
        double normB = 0.0;
        for (int i = 0; i < vectorA.size(); i++) {
            dotProduct += vectorA.get(i) * vectorB.get(i);
            normA += Math.pow(vectorA.get(i), 2);
            normB += Math.pow(vectorB.get(i), 2);
        }
        return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
    }
}

Token管理

Tokens是AI模型工作的基础。在输入时,模型将单词转换为Tokens;在输出时,它们将Tokens转换回单词。在英语中,一个Token大约对应75%的单词。

Tokens与成本

在托管AI模型的上下文中,费用是根据使用的Tokens数量计算的。输入和输出都会贡献到总的Tokens计数。此外,模型有Token限制,这限制了单次API调用中处理的文本量。

Token限制示例

例如,ChatGPT3有4K的Token限制,而GPT4提供了不同的选项。管理好Token的使用,可以有效地控制成本并提高模型的效率。

计算Tokens示例

以下是一个计算输入文本中Tokens数量的示例:

import org.springframework.stereotype.Service;
import com.example.springai.OpenAiTokenService;

@Service
public class TokenService {

    private final OpenAiTokenService tokenService;

    public TokenService(OpenAiTokenService tokenService) {
        this.tokenService = tokenService;
    }

    public int countTokens(String text) {
        return tokenService.countTokens(text);
    }
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TokenController {

    @Autowired
    private TokenService tokenService;

    @GetMapping("/count-tokens")
    public int countTokens(@RequestParam String text) {
        return tokenService.countTokens(text);
    }
}

结论

通过理解嵌入技术和Token管理,你可以更有效地使用Spring AI构建高效的AI应用。嵌入技术帮助AI模型更好地理解和处理语言数据,而Token管理可以控制成本并提高效率。希望这篇文章能帮助你在实际项目中应用这些技术,并激发你更多的创意。

下一篇文章中,我们将继续探讨更多实际应用场景和高级功能,帮助你进一步掌握这一强大的工具。

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐