SoFunction
Updated on 2025-04-11

Go language uses aicli to easily call DeepSeek and ChatGPT

In the context of the rapid development of artificial intelligence today, how to easily call intelligent dialogue models in projects has become a hot topic of concern to developers. Today, we will discuss a library of AI assistant client written in Go language - aicli. This library not only supports ChatGPT, but also integrates DeepSeek to help developers quickly access AI capabilities in their projects. This article will introduce in detail how aicli works and its usage examples, taking you from getting started to practice and experience the charm of AI assistant integration.

Overview of the principles of aicli library

The original intention of the aicli library is to simplify the calling process of AI assistant services. It encapsulates the underlying API request logic, and provides a unified interface so that developers can call services such as ChatGPT and DeepSeek without paying attention to specific details. Its core principle is:

  • Client Initialization: By passing in API Key, a connection with the AI ​​server will be established within the library, and different model parameters will be set according to user needs, such as default model, streaming and other options. This design not only improves the reusability of the code, but also makes it easier to subsequently extend other models.

  • Synchronous and streaming calls: aicli provides two ways of calling. One is to call synchronously, throughSendThe method sends a request and waits for the complete response; the other is a streaming call, throughSendStreamMethod gets the segmented response. This flexible design allows developers to choose different calling methods according to actual needs, which can not only obtain fast responses, but also display the process of long text generation in real time.

  • Context Management: In Go language, useCarrying out request management has become a best practice. The aicli library makes full use of this mechanism to ensure that requests can be processed in time when timeout or cancellation, improving the robustness and reliability of the system.

Use example analysis

Let's take a look at two typical usage examples, one is a ChatGPT-based call, and the other is a DeepSeek-based call.

ChatGPT call example

package main

import (
    "context"
    "fmt"
    "/go-dev-frame/sponge/pkg/aicli/chatgpt"
)

func main() {
    var apiKey = "sk-xxxxxx"
    client, _ := (apiKey) // You can set client options, such as WithModel(ModelGPT4o)
    // Example 1: Synchronous call    content, _ := ((), "Who are you?")
    (content)

    // Example 2: Streaming call    answer := ((), "Which model did you use to answer the question?")
    for content := range  {
        (content)
    }
    if  != nil {
        panic()
    }
}

In this example, we first pass(apiKey)A ChatGPT client was created. Then, byMethod send the question "Who are you?" and receive answers simultaneously. To show the streaming response, we called againMethod, gradually output the answer content in the loop. This step-by-step output method is very practical when dealing with large text or displaying generated content in real time.

DeepSeek call example

package main

import (
    "context"
    "fmt"
    "/go-dev-frame/sponge/pkg/aicli/deepseek"
)

func main() {
    var apiKey = "sk-xxxxxx"
    client, _ := (apiKey) // You can set client options, such as WithModel(ModelDeepSeekReasoner)
    // Example 1: Synchronous call    content, _ := ((), "Who are you?")
    (content)

    // Example 2: Streaming call    answer := ((), "Which model did you use to answer the question?")
    for content := range  {
        (content)
    }
    if  != nil {
        panic()
    }
}

This example is very similar to the usage of ChatGPT. The main difference is that the DeepSeek service is called, and developers can also choose synchronization or streaming methods to get answers according to their needs. Through such a design, the aicli library realizes a unified encapsulation of different AI services, so that developers do not need to modify a large amount of code when switching different models, greatly improving development efficiency.

Summarize

The aicli library provides Go language developers with a simple and efficient way to integrate AI conversation capabilities. Whether it is synchronous calls or streaming calls, this library can meet the developers' dual needs for real-time and reliability. By encapsulating the details of the underlying API call, aicli allows you to focus on the implementation of business logic without worrying about complex network request management. Whether it is building an intelligent customer service system, an automated question-and-answer platform, or other projects that require natural language processing capabilities, aicli is a worthwhile choice.

This is the article about Go using aicli to easily call DeepSeek and ChatGPT. For more information about Go aicli calling DeepSeek and ChatGPT, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!