当前位置:   article > 正文

阿里通义千问大模型和esp32的对接_通义千问对接

通义千问对接

目录


通义千问

通义千问是阿里云自主研发的超大规模语言模型,能够在用户自然语言输入的基础上,通过自然语言理解和语义分析,在不同领域、任务内为用户提供服务和帮助。

模型具备的能力包括但不限于:

创作文字,如写故事、写公文、写邮件、写剧本、写诗歌等

编写代码

提供各类语言的翻译服务,如英语、日语、法语、西班牙语等

进行文本润色和文本摘要等工作

扮演角色进行对话

制作图表


一、快速开始

前提条件

通义千问

已开通服务并获得API-KEY:开通DashScope并创建API-KEY。

已安装最新版SDK:安装DashScope SDK。

这里post上地址

开通DashScope并创建API-KEY

安装DashScope SDK(不是必须,提供了python和java的接口)

二、初步验证

1.示例代码

以下示例展示了调用通义千问API对一个用户指令进行响应的代码。说明
需要使用您的API-KEY替换示例中的 YOUR_DASHSCOPE_API_KEY,代码才能正常运行。
python sdk version: dashscope>=1.10.0
java sdk version: >=2.5.0:

export DASHSCOPE_API_KEY=YOUR_DASHSCOPE_API_KEY
例如:export DASHSCOPE_API_KEY=sk-12341234123412341234123412341234
这个需要自己在上面的地址中申请下来,面向用户是免费半年,半年后收费1000字大概是0.012元,一分多还好
  • 1
  • 2
  • 3

代码如下(示例):

from http import HTTPStatus
from dashscope import Generation


def call_with_messages():
    messages = [{'role': 'system', 'content': '你是生活助手机器人。'},
                {'role': 'user', 'content': '如何做西红柿鸡蛋?'}]
    gen = Generation()
    response = gen.call(
        Generation.Models.qwen_turbo,
        messages=messages,
        result_format='message', # set the result is message format.
    )
    if response.status_code == HTTPStatus.OK:   
        print(response)  
    else:
        print('Request id: %s, Status code: %s, error code: %s, error message: %s'%(
            response.request_id, response.status_code, 
            response.code, response.message
        ))     

if __name__ == '__main__':
    call_with_messages()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

如上证明其可用。

ESP32实装(esp-idf v4.4)

提示:esp-idf需要自己安装,安装可以参考这个
esp-idf安装教程

我们使用/home/coldj/Desktop/esp-idf-4.4/examples/protocols/esp_http_client这个demo进行测试。

首先menuconfig 设置你的wifi账号密码

在这里插入图片描述
然后post上下面的代码

注意需要把下文中的header里面的Authorization更换成你的sk-12341234123412341234123412341234

static void http_native_request2(void)
{
    // Declare local_response_buffer with size (MAX_HTTP_OUTPUT_BUFFER + 1) to prevent out of bound access when
    // it is used by functions like strlen(). The buffer should only be used upto size MAX_HTTP_OUTPUT_BUFFER
    char output_buffer[MAX_HTTP_OUTPUT_BUFFER + 1] = {0};   // Buffer to store response of http request
    int content_length = 0;
    esp_http_client_config_t config = {
        .url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation",
    };
    esp_http_client_handle_t client = esp_http_client_init(&config);
    // POST Request
    const char *post_data =  "{\"model\":\"qwen-turbo\",\"input\":{\"messages\":[{\"role\": \"system\",\"content\": \"你是达摩院的生活助手机器人。\"},{\"role\": \"user\",\"content\": \"你好,附近哪里有博物馆?\"}]}}";
    esp_http_client_set_url(client, "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation");
    esp_http_client_set_method(client, HTTP_METHOD_POST);
    esp_http_client_set_header(client, "Content-Type", "application/json");
    esp_http_client_set_header(client, "Authorization", "sk-12341234123412341234123412341234");
    esp_err_t err = esp_http_client_open(client, strlen(post_data));
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
    } else {
        int wlen = esp_http_client_write(client, post_data, strlen(post_data));
        if (wlen < 0) {
            ESP_LOGE(TAG, "Write failed");
        }
        content_length = esp_http_client_fetch_headers(client);
        if (content_length < 0) {
            ESP_LOGE(TAG, "HTTP client fetch headers failed");
        } else {
            int data_read = esp_http_client_read_response(client, output_buffer, MAX_HTTP_OUTPUT_BUFFER);
            if (data_read >= 0) {
                ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %"PRId64,
                esp_http_client_get_status_code(client),
                esp_http_client_get_content_length(client));
                 ESP_LOGI(TAG, "HTTP POST output_buffer = %s\r\n",output_buffer);
                ESP_LOG_BUFFER_HEX(TAG, output_buffer, strlen(output_buffer));
            } else {
                ESP_LOGE(TAG, "Failed to read response");
            }
        }
    }
    esp_http_client_cleanup(client);
}

static void http_test_task(void *pvParameters)
{
    http_native_request2();
    ESP_LOGI(TAG, "Finish http example");
    vTaskDelete(NULL);
}

void app_main(void)
{
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
      ESP_ERROR_CHECK(nvs_flash_erase());
      ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);
    ESP_ERROR_CHECK(esp_netif_init());
    ESP_ERROR_CHECK(esp_event_loop_create_default());

    /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
     * Read "Establishing Wi-Fi or Ethernet Connection" section in
     * examples/protocols/README.md for more information about this function.
     */
    ESP_ERROR_CHECK(example_connect());
    ESP_LOGI(TAG, "Connected to AP, begin http example");

    xTaskCreate(&http_test_task, "http_test_task", 8192, NULL, 5, NULL);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

让我们看下输出:

rst:0x1 (POWERON),boot:0xc (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fcd6100,len:0x16c0
load:0x403ce000,len:0x930
load:0x403d0000,len:0x2d40
entry 0x403ce000
I (30) boot: ESP-IDF v4.4-dirty 2nd stage bootloader
I (30) boot: compile time 19:47:08
I (30) boot: chip revision: 3
I (32) boot.esp32c3: SPI Speed      : 80MHz
I (37) boot.esp32c3: SPI Mode       : DIO
I (42) boot.esp32c3: SPI Flash Size : 2MB
I (46) boot: Enabling RNG early entropy source...
I (52) boot: Partition Table:
I (55) boot: ## Label            Usage          Type ST Offset   Length
I (63) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (70) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (78) boot:  2 factory          factory app      00 00 00010000 00100000
I (85) boot: End of partition table
I (89) esp_image: segment 0: paddr=00010020 vaddr=3c0a0020 size=1a860h (108640) map
I (115) esp_image: segment 1: paddr=0002a888 vaddr=3fc8e200 size=02b94h ( 11156) load
I (117) esp_image: segment 2: paddr=0002d424 vaddr=40380000 size=02bf4h ( 11252) load
I (123) esp_image: segment 3: paddr=00030020 vaddr=42000020 size=97d8ch (621964) map
I (226) esp_image: segment 4: paddr=000c7db4 vaddr=40382bf4 size=0b514h ( 46356) load
I (235) esp_image: segment 5: paddr=000d32d0 vaddr=50000010 size=00010h (    16) load
I (240) boot: Loaded app from partition at offset 0x10000
I (240) boot: Disabling RNG early entropy source...
I (255) cpu_start: Pro cpu up.
I (264) cpu_start: Pro cpu start user code
I (264) cpu_start: cpu freq: 160000000
I (264) cpu_start: Application information:
I (266) cpu_start: Project name:     esp_http_client_example
I (273) cpu_start: App version:      v4.4-dirty
I (278) cpu_start: Compile time:     Sep 14 2023 19:47:03
I (284) cpu_start: ELF file SHA256:  1389712bbca9ace7...
I (290) cpu_start: ESP-IDF:          v4.4-dirty
I (295) heap_init: Initializing. RAM available for dynamic allocation:
I (303) heap_init: At 3FC94E40 len 0002B1C0 (172 KiB): DRAM
I (309) heap_init: At 3FCC0000 len 0001F060 (124 KiB): STACK/DRAM
I (316) heap_init: At 50000020 len 00001FE0 (7 KiB): RTCRAM
I (322) spi_flash: detected chip: mxic
I (326) spi_flash: flash io: dio
W (330) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (344) sleep: Configure to isolate all GPIO pins in sleep state
I (350) sleep: Enable automatic switching of GPIO sleep configuration
I (357) cpu_start: Starting scheduler.
I (368) pp: pp rom version: 9387209
I (368) net80211: net80211 rom version: 9387209
I (378) wifi:wifi driver task: 3fc9d8b8, prio:23, stack:6656, core=0
I (378) system_api: Base MAC address is not set
I (388) system_api: read default base MAC address from EFUSE
I (398) wifi:wifi firmware version: 7679c42
I (398) wifi:wifi certification version: v7.0
I (398) wifi:config NVS flash: enabled
I (398) wifi:config nano formating: disabled
I (408) wifi:Init data frame dynamic rx buffer num: 32
I (408) wifi:Init management frame dynamic rx buffer num: 32
I (418) wifi:Init management short buffer num: 32
I (418) wifi:Init dynamic tx buffer num: 32
I (428) wifi:Init static tx FG buffer num: 2
I (428) wifi:Init static rx buffer size: 1600
I (428) wifi:Init static rx buffer num: 10
I (438) wifi:Init dynamic rx buffer num: 32
I (438) wifi_init: rx ba win: 6
I (448) wifi_init: tcpip mbox: 32
I (448) wifi_init: udp mbox: 6
I (448) wifi_init: tcp mbox: 6
I (458) wifi_init: tcp tx win: 5744
I (458) wifi_init: tcp rx win: 5744
I (468) wifi_init: tcp mss: 1440
I (468) wifi_init: WiFi IRAM OP enabled
I (468) wifi_init: WiFi RX IRAM OP enabled
I (478) example_connect: Connecting to yanfawifi...
I (478) phy_init: phy_version 907,3369105-dirty,Dec  3 2021,14:55:12
I (538) wifi:mode : sta (60:55:f9:79:8a:28)
I (538) wifi:enable tsf
I (538) example_connect: Waiting for IP(s)
I (2588) wifi:new:<11,2>, old:<1,0>, ap:<255,255>, sta:<11,2>, prof:1
I (3198) wifi:state: init -> auth (b0)
I (3268) wifi:state: auth -> assoc (0)
I (3298) wifi:state: assoc -> run (10)
I (3418) wifi:connected with yanfawifi, aid = 1, channel 11, 40D, bssid = 44:df:65:a5:08:6f
I (3418) wifi:security: WPA2-PSK, phy: bgn, rssi: -18
I (3418) wifi:pm start, type: 1

I (3418) wifi:set rx beacon pti, rx_bcn_pti: 0, bcn_timeout: 0, mt_pti: 25000, mt_time: 10000
W (3468) wifi:<ba-add>idx:0 (ifx:0, 44:df:65:a5:08:6f), tid:6, ssn:1, winSize:64
I (3488) wifi:BcnInt:102400, DTIM:1
I (4368) esp_netif_handlers: example_connect: sta ip: 192.168.2.84, mask: 255.255.255.0, gw: 192.168.2.1
I (4368) example_connect: Got IPv4 event: Interface "example_connect: sta" address: 192.168.2.84
I (5368) example_connect: Got IPv6 event: Interface "example_connect: sta" address: fe80:0000:0000:0000:6255:f9ff:fe79:8a28, type: ESP_IP6_ADDR_IS_LINK_LOCAL
I (5368) example_connect: Connected to example_connect: sta
I (5378) example_connect: - IPv4 address: 192.168.2.84
I (5378) example_connect: - IPv6 address: fe80:0000:0000:0000:6255:f9ff:fe79:8a28, type: ESP_IP6_ADDR_IS_LINK_LOCAL
I (5388) HTTP_CLIENT: Connected to AP, begin http example
W (5398) wifi:<ba-add>idx:1 (ifx:0, 44:df:65:a5:08:6f), tid:0, ssn:3, winSize:64
I (9238) HTTP_CLIENT: HTTP POST Status = 200, content_length = -4294967005
I (9238) HTTP_CLIENT: HTTP POST output_buffer = {"output":
{"finish_reason":"stop",
"text":"你好!附近的博物馆有上海博物馆、上海科技馆、上海自然博物馆等。你想了解更多关于这些博物馆的信息吗?"},
"usage":{"output_tokens":48,"input_tokens":38},
"request_id":"12341234-692b-996c-88f7-c22eeb5cab3f"}
I (9398) HTTP_CLIENT: Finish http example
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104

总结

后续可以通过其他方法对音频进行识别,识别后取回数据,在通过TTS进行语音合成,之后就可以形成一个低端芯片的智能家居中控了。

后续这个key也可以通过云平台和IOT方面对接形成规则,直接进行和IOT方面相关的合并。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/869714
推荐阅读
  

闽ICP备14008679号