Cohere API Swift 包

Cohere API Swift 包是一个全面的 Swift 库,它提供了与 Cohere API 的无缝集成。它允许开发者在其 Swift 项目中轻松利用 Cohere API 提供的全部特性和功能。有关 Cohere AI 的更多信息,请参阅Cohere 关于页面

非常重要:流模式目前无法正常工作 😢

要求

主要特性

完整的 API 覆盖:此 Swift 包实现了 Cohere API 的所有特性和功能,确保您可以访问 Cohere 强大的自然语言处理平台提供的全部功能。

入门指南

要开始使用 Cohere API Swift 包,只需按照以下步骤操作

安装

使用 Swift Package Manager (SPM) 安装该包,方法是将其作为依赖项添加到项目的 Package.swift 文件中。

用法

在您的项目中导入 Cohere API Swift 包,并通过以下方法获取 API 响应。

分类

此方法生成关于哪个标签最适合指定文本输入的预测。

import Cohere

let co = CohereClient(API_KEY: "<API_KEY>")

Task.init {
    if let classification = await co.classify(
        model:
            .command,
        inputs: [
            "Confirm your email address",
            "hey i need u to send some $"
        ],
        examples:
        [
            Example(text: "Dermatologists don't like her!", label: "Spam"),
            Example(text: "Hello, open to this?", label: "Spam"),
            Example(text: "I need help please wire me $1000 right now", label: "Spam"),
            Example(text: "Nice to know you ;)", label: "Spam"),
            Example(text: "Please help me?", label: "Spam"),
            Example(text: "Your parcel will be delivered today", label: "Not spam"),
            Example(text: "Review changes to our Terms and Conditions", label: "Not spam"),
            Example(text: "Weekly sync notes", label: "Not spam"),
            Example(text: "Re: Follow up from today’s meeting", label: "Not spam"),
            Example(text: "Pre-read for tomorrow", label: "Not spam")
        ]) {
        for i in classification.classifications! {
            print(i.prediction!)
        }
    }
}

示例输出

"Not spam"
"Spam"

注意

生成

此方法生成以给定输入为条件的逼真文本。

import Cohere

let co = CohereClient(API_KEY: "<API_KEY>")

Task.init {
    if let res = await co.generate(prompt: "Please explain to me how LLMs work", model: .command, generations: 1, stream: false, temperature: 0.75) {
        print(res.generations?.first?.text)
    }
}

示例输出

"LLMs, or Large Language Models, are a type of neural network..."

注意

嵌入

此方法生成关于文本的语义信息,可用于分类文本。

import Cohere

let co = CohereClient(API_KEY: "<API_KEY>")

co.embed(texts: ["hello", "goodbye"], model: .englishV2, inputType: .classificatinon) { response in
    if let _response = response.embeddings {
        print(_response) 
    }
}

示例输出

[[1.6142578, 0.24841309, 0.5385742, -1.6630859, -0.27783203, 0.35888672,...]] 

注意

聊天

聊天端点允许用户与 Cohere 的大型语言模型 (LLM) 进行对话。 如果使用默认方法,则与机器人之间的消息将保存到历史记录中。 可以使用 .clearChatHistory 方法清除历史记录。 您也可以提供现有对话的 ID 以恢复聊天。 如果您提供的 ID 尚不存在,则将创建一个新对话。

import Cohere

let co = CohereClient(API_KEY: "<API_KEY>")

Task.init {
    if let res = await co.addMessage(message: "Hi my name is Moddingus", model: .command) {
        print(res.text)
    }
    if let res = await co.addMessage(message: "Say my name", model: .command) {
        print(res.text)
    }
}

示例输出

"Nice to meet you, Moddingus!..."

"Sure, my friend. Let me proudly pronounce your name -- Moddingus. \n\nNow, how does it feel to hear your name being gracefully expressed by an AI chatbot? \n\nWould you like me to repeatedly chant your name in a poetic manner, or should I move on to another topic of your choice?..."

注意

有关更多信息和详细的使用示例,请参阅Cohere API 文档

支持和反馈

如果您遇到任何问题或对 Cohere API Swift 包有任何疑问或反馈,请随时联系我们的支持团队(只有我)或在包的 GitHub 存储库上打开一个 issue。 欢迎 fork 并对此包进行 PR,我会尽力处理它们,你们可能比我更擅长这个。