Xamarin.Android实现本地化部署DeepSeek的对话功能
1、背景
时间飞逝啊!春节期间大火的DeepSeek,假期还没结束,就赶紧自己部署了一个R1的最低版本(1.5B),顺便在App上通过API实现与本地DeepSeek的对话功能。这几天睡眠不好,写了几篇,正好总结及记录一下吧
2、效果
整体的效果如视频所示。【说明】当时整的时候,只是一个最简单的http请求,没有使用SignalR
,改天再加上试试
Xamarin.Android实现DeepSeek的对话功能
3、具体实现
3.1 .9.png图(点九图)
首先是对话框,就是下图中的聊天框的蓝色和灰色的区域,在Android开发中经常使用后缀名称为.9.png的图片,作为存放文字的区域。
先说一下.9.png的逻辑
上面的横线,代表整个对话框左右移动的区域,左侧是上下移动的区域;而右面的横线则是文字在垂直方向的存放区域,下方的则是文字在水平方向的存放区域。大体如下图所示
如何画出这个界面,网上的方法有很多,这里介绍一个很简单的。登录这个网址:Android Asset Studio,选择这个,进行尝试即可,非常简单。这个网站还有很多其他功能,甚至好用。
将制作完成的.9.png图片导入至项目中既可。左右各一个.9图
这样,前期的准备工作基本完成了
3.2 App界面实现
整体界面很简单,整体的布局示意图如图所示
主要分为两个文件实现:deepseek_layout.xml
和deepseek_dialog_layout.xml
,其中前者为整体的布局文件,后者为ListView的adapter的配套布局文件。两者的代码如下:
deepseek_layout.xml
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#d8e0e8"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/msg_list_view"
android:divider="#0000"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input_text"
android:hint="请输入您的问题..."
android:maxLines="2"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="0dp" />
<Button
android:id="@+id/send_btn"
android:text="发送"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
deepseek_dialog_layout.xml
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<TextView
android:textAlignment="center"
android:id="@+id/currentDateTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="@+id/left_layout"
android:layout_gravity="left"
android:background="@drawable/msg_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_gravity="center"
android:layout_margin="10dp"
android:textColor="#fff"
android:id="@+id/left_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:id="@+id/right_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@drawable/msg_right" >
<TextView
android:id="@+id/right_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp" />
</LinearLayout>
</LinearLayout>
然后deepseek的请求,并完成ListView的赋值。关于deepseek的接口调用,参见使用C#控制台调用本地部署的DeepSeek。最终完成本项目的效果。
4、参考资料
1、.9.PNG是啥?