【Tauri】(4):整合Tauri和actix-web做本地大模型应用开发,可以实现session 登陆接口,完成页面展示,进入聊天界面

1,视频地址

https://www.bilibili.com/video/BV1GJ4m1Y7Aj/

【Tauri】(4):整合Tauri和actix-web做本地大模型应用开发,可以实现session 登陆接口,完成页面展示,进入聊天界面

使用国内代理进行加速

配置rust环境方法

https://rsproxy.cn/

特别注意要配置好:

步骤三:设置 crates.io 镜像, 修改配置 ~/.cargo/config,已支持git协议和sparse协议,>=1.68 版本建议使用 sparse-index,速度更快。

[source.crates-io]
replace-with = 'rsproxy-sparse'
[source.rsproxy]
registry = "https://rsproxy.cn/crates.io-index"
[source.rsproxy-sparse]
registry = "sparse+https://rsproxy.cn/index/"
[registries.rsproxy]
index = "https://rsproxy.cn/crates.io-index"
[net]
git-fetch-with-cli = true

2,然后开始编写 http 服务

整合 tauri 代码和 actix-web,入门文档:
https://juejin.cn/post/7342858918161793059

参考文章:

https://blog.moonguard.dev/setting-up-actix-in-tauri

主要就是:

fn main() {
    tauri::Builder::default()
        .setup(|app| {

            let handle = app.handle();
            let boxed_handle = Box::new(handle);

            thread::spawn(move || {
                server::init(*boxed_handle).unwrap();
            });
            Ok(())
        })
        .invoke_handler(tauri::generate_handler![greet])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

实现了 server::init

然后增加 controller :

#[post("/api/session")]
pub async fn session_handle() -> actix_web::Result<String> {
  let text = "{\"status\":\"Success\", \"message\":\"\", \"data\": {\"auth\": false, \"model\":\"chatglm3\"}}";
  println!("{}",text);
  Ok(text.to_string())
}

这样就可以登陆成功了:

在这里插入图片描述

3,接下来就是实现 chat 聊天接口了

需要返回标准的 openai api 接口了。

4,解决跨域问题

https://docs.rs/actix-cors/latest/actix_cors/

使用 actix-cors 进行跨域处理:

use actix_cors::Cors;
use actix_web::{get, http, web, App, HttpRequest, HttpResponse, HttpServer};

#[get("/index.html")]
async fn index(req: HttpRequest) -> &'static str {
    "<p>Hello World!</p>"
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        let cors = Cors::default()
              .allowed_origin("https://www.rust-lang.org")
              .allowed_origin_fn(|origin, _req_head| {
                  origin.as_bytes().ends_with(b".rust-lang.org")
              })
              .allowed_methods(vec!["GET", "POST"])
              .allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
              .allowed_header(http::header::CONTENT_TYPE)
              .max_age(3600);

        App::new()
            .wrap(cors)
            .service(index)
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await;

    Ok(())
}

最近更新

  1. TCP协议是安全的吗?

    2024-03-10 14:22:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-10 14:22:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-10 14:22:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-10 14:22:02       20 阅读

热门阅读

  1. Unity3D 实现大世界地图的技术原理详解

    2024-03-10 14:22:02       20 阅读
  2. IOS面试题object-c 1-10

    2024-03-10 14:22:02       23 阅读
  3. iOS面试题

    2024-03-10 14:22:02       22 阅读
  4. [论文笔记] Open-sora 2、视频数据集介绍 MSR-VTT

    2024-03-10 14:22:02       23 阅读
  5. android 快速实现 recyclerview 的所有item 都执行动画

    2024-03-10 14:22:02       21 阅读
  6. 2024.3.9 C++启航 梦开始的地方

    2024-03-10 14:22:02       23 阅读
  7. 博客杂谈---程序员如何选择职业赛道?

    2024-03-10 14:22:02       22 阅读