├── 实战案例2 ├── __init__.py ├── 整合了案例2的实战案例1 │ ├── home │ │ ├── __init__.py │ │ ├── migrations │ │ │ ├── __init__.py │ │ │ ├── 0004_对话记录_已结束.py │ │ │ ├── 0003_对话记录_不带入大模型对话中.py │ │ │ ├── 0005_rename_展示content_对话记录_处理后content.py │ │ │ ├── 0008_alter_cnet新闻_新闻发布日期.py │ │ │ ├── 0002_对话记录.py │ │ │ ├── 0006_对话记录_提交给大模型的playload_alter_对话记录_content_and_more.py │ │ │ ├── 0007_cnet新闻.py │ │ │ └── 0001_initial.py │ │ ├── tests.py │ │ ├── apps.py │ │ ├── admin.py │ │ ├── search.py │ │ ├── urls.py │ │ ├── models.py │ │ ├── views_api.py │ │ └── views.py │ ├── mysite │ │ ├── __init__.py │ │ ├── asgi.py │ │ ├── wsgi.py │ │ └── urls.py │ ├── requirements.txt │ ├── manage.py │ └── templates │ │ └── home │ │ ├── index.html │ │ ├── addsalescheck.html │ │ └── salescheck.html ├── feed.bat ├── 将新闻导入数据库.py ├── 新闻.py ├── rag.py ├── 生成简报.py └── feed.py ├── 实战案例1 ├── 改造前 │ ├── home │ │ ├── __init__.py │ │ ├── migrations │ │ │ ├── __init__.py │ │ │ └── 0001_initial.py │ │ ├── admin.py │ │ ├── tests.py │ │ ├── apps.py │ │ ├── search.py │ │ ├── urls.py │ │ ├── models.py │ │ └── views.py │ ├── mysite │ │ ├── __init__.py │ │ ├── asgi.py │ │ ├── wsgi.py │ │ ├── urls.py │ │ └── settings.py │ ├── requirements.txt │ ├── templates │ │ └── home │ │ │ ├── index.html │ │ │ ├── addsalescheck.html │ │ │ └── salescheck.html │ └── manage.py └── 改造后 │ ├── home │ ├── __init__.py │ ├── migrations │ │ ├── __init__.py │ │ ├── 0003_对话记录_不带入大模型对话中.py │ │ ├── 0004_对话记录_已结束.py │ │ ├── 0005_rename_展示content_对话记录_处理后content.py │ │ ├── 0002_对话记录.py │ │ ├── 0006_对话记录_提交给大模型的playload_alter_对话记录_content_and_more.py │ │ └── 0001_initial.py │ ├── tests.py │ ├── apps.py │ ├── search.py │ ├── admin.py │ ├── urls.py │ ├── models.py │ ├── views_api.py │ └── views.py │ ├── mysite │ ├── __init__.py │ ├── asgi.py │ ├── wsgi.py │ ├── urls.py │ └── settings.py │ ├── requirements.txt │ ├── manage.py │ └── templates │ └── home │ ├── index.html │ ├── addsalescheck.html │ └── salescheck.html ├── 实战案例3 ├── 第18节 │ └── main │ │ ├── README.md │ │ ├── home │ │ ├── __init__.py │ │ ├── migrations │ │ │ ├── __init__.py │ │ │ ├── 0003_对话记录_不带入大模型对话中.py │ │ │ ├── 0004_对话记录_已结束.py │ │ │ ├── 0005_rename_展示content_对话记录_处理后content.py │ │ │ ├── 0008_alter_对话记录_id_alter_销售入账记录_id.py │ │ │ ├── 0007_销售入账记录_客户向量编码_销售入账记录_客户向量编码模型.py │ │ │ ├── 0002_对话记录.py │ │ │ ├── 0006_对话记录_提交给大模型的playload_alter_对话记录_content_and_more.py │ │ │ └── 0001_initial.py │ │ ├── tests.py │ │ ├── apps.py │ │ ├── admin.py │ │ ├── api.py │ │ ├── urls.py │ │ ├── models.py │ │ ├── views_api.py │ │ ├── search.py │ │ └── views.py │ │ ├── mysite │ │ ├── __init__.py │ │ ├── asgi.py │ │ ├── wsgi.py │ │ └── urls.py │ │ ├── requirements.txt │ │ ├── manage.py │ │ └── templates │ │ └── home │ │ ├── index.html │ │ ├── addsalescheck.html │ │ └── salescheck.html ├── 第19节 │ ├── 程序 │ │ └── main │ │ │ ├── README.md │ │ │ ├── home │ │ │ ├── __init__.py │ │ │ ├── migrations │ │ │ │ ├── __init__.py │ │ │ │ ├── 0012_remove_知识主表_创建时间.py │ │ │ │ ├── 0004_对话记录_已结束.py │ │ │ │ ├── 0003_对话记录_不带入大模型对话中.py │ │ │ │ ├── 0005_rename_展示content_对话记录_处理后content.py │ │ │ │ ├── 0008_alter_对话记录_id_alter_销售入账记录_id.py │ │ │ │ ├── 0010_知识详细表_内容在分段中的顺序_alter_知识详细表_知识主表.py │ │ │ │ ├── 0007_销售入账记录_客户向量编码_销售入账记录_客户向量编码模型.py │ │ │ │ ├── 0002_对话记录.py │ │ │ │ ├── 0006_对话记录_提交给大模型的playload_alter_对话记录_content_and_more.py │ │ │ │ ├── 0011_知识主表_模块_alter_知识主表_url_alter_知识主表_标题.py │ │ │ │ ├── 0013_remove_知识主表_created_time_and_more.py │ │ │ │ ├── 0001_initial.py │ │ │ │ └── 0009_知识主表_知识详细表.py │ │ │ ├── tests.py │ │ │ ├── apps.py │ │ │ ├── admin.py │ │ │ ├── urls.py │ │ │ ├── views_api.py │ │ │ ├── search.py │ │ │ ├── api.py │ │ │ ├── models.py │ │ │ └── views.py │ │ │ ├── mysite │ │ │ ├── __init__.py │ │ │ ├── asgi.py │ │ │ ├── wsgi.py │ │ │ └── urls.py │ │ │ ├── requirements.txt │ │ │ ├── manage.py │ │ │ └── templates │ │ │ └── home │ │ │ ├── index.html │ │ │ ├── addsalescheck.html │ │ │ └── salescheck.html │ ├── result.png │ ├── 要导入的数据 │ │ ├── data.xlsx │ │ └── import_data_from_excel.py │ └── 如何运行本节课程序.md ├── 第20节 │ ├── 程序 │ │ └── main │ │ │ ├── README.md │ │ │ ├── home │ │ │ ├── __init__.py │ │ │ ├── migrations │ │ │ │ ├── __init__.py │ │ │ │ ├── 0012_remove_知识主表_创建时间.py │ │ │ │ ├── 0004_对话记录_已结束.py │ │ │ │ ├── 0003_对话记录_不带入大模型对话中.py │ │ │ │ ├── 0005_rename_展示content_对话记录_处理后content.py │ │ │ │ ├── 0008_alter_对话记录_id_alter_销售入账记录_id.py │ │ │ │ ├── 0014_对话记录_negative_review_对话记录_positive_review.py │ │ │ │ ├── 0010_知识详细表_内容在分段中的顺序_alter_知识详细表_知识主表.py │ │ │ │ ├── 0007_销售入账记录_客户向量编码_销售入账记录_客户向量编码模型.py │ │ │ │ ├── 0002_对话记录.py │ │ │ │ ├── 0006_对话记录_提交给大模型的playload_alter_对话记录_content_and_more.py │ │ │ │ ├── 0011_知识主表_模块_alter_知识主表_url_alter_知识主表_标题.py │ │ │ │ ├── 0013_remove_知识主表_created_time_and_more.py │ │ │ │ ├── 0001_initial.py │ │ │ │ └── 0009_知识主表_知识详细表.py │ │ │ ├── tests.py │ │ │ ├── apps.py │ │ │ ├── admin.py │ │ │ ├── urls.py │ │ │ ├── views_api.py │ │ │ ├── search.py │ │ │ ├── api.py │ │ │ ├── models.py │ │ │ └── views.py │ │ │ ├── mysite │ │ │ ├── __init__.py │ │ │ ├── asgi.py │ │ │ ├── wsgi.py │ │ │ └── urls.py │ │ │ ├── requirements.txt │ │ │ ├── manage.py │ │ │ └── templates │ │ │ └── home │ │ │ ├── index.html │ │ │ ├── addsalescheck.html │ │ │ └── salescheck.html │ ├── result.png │ └── 如何运行本节课程序.md ├── 第21节 │ ├── 程序 │ │ └── main │ │ │ ├── README.md │ │ │ ├── home │ │ │ ├── __init__.py │ │ │ ├── migrations │ │ │ │ ├── __init__.py │ │ │ │ ├── 0012_remove_知识主表_创建时间.py │ │ │ │ ├── 0004_对话记录_已结束.py │ │ │ │ ├── 0003_对话记录_不带入大模型对话中.py │ │ │ │ ├── 0005_rename_展示content_对话记录_处理后content.py │ │ │ │ ├── 0016_remove_关键词_created_time_remove_关键词_lastmodified_time.py │ │ │ │ ├── 0008_alter_对话记录_id_alter_销售入账记录_id.py │ │ │ │ ├── 0014_对话记录_negative_review_对话记录_positive_review.py │ │ │ │ ├── 0010_知识详细表_内容在分段中的顺序_alter_知识详细表_知识主表.py │ │ │ │ ├── 0007_销售入账记录_客户向量编码_销售入账记录_客户向量编码模型.py │ │ │ │ ├── 0002_对话记录.py │ │ │ │ ├── 0015_关键词.py │ │ │ │ ├── 0006_对话记录_提交给大模型的playload_alter_对话记录_content_and_more.py │ │ │ │ ├── 0011_知识主表_模块_alter_知识主表_url_alter_知识主表_标题.py │ │ │ │ ├── 0013_remove_知识主表_created_time_and_more.py │ │ │ │ ├── 0001_initial.py │ │ │ │ └── 0009_知识主表_知识详细表.py │ │ │ ├── tests.py │ │ │ ├── apps.py │ │ │ ├── admin.py │ │ │ ├── prompt.py │ │ │ ├── urls.py │ │ │ ├── views_api.py │ │ │ ├── search.py │ │ │ ├── api.py │ │ │ ├── models.py │ │ │ └── views.py │ │ │ ├── mysite │ │ │ ├── __init__.py │ │ │ ├── asgi.py │ │ │ ├── wsgi.py │ │ │ └── urls.py │ │ │ ├── requirements.txt │ │ │ ├── manage.py │ │ │ └── templates │ │ │ └── home │ │ │ ├── addsalescheck.html │ │ │ ├── salescheck.html │ │ │ └── index.html │ ├── result.png │ └── 要导入的数据 │ │ ├── data.xlsx │ │ └── import_data_from_excel.py └── 嵌入模型服务 │ ├── download.py │ ├── main.py │ └── 向量编码器.py ├── README.md └── .vscode └── settings.json /实战案例2/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例1/改造前/home/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例1/改造前/mysite/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例1/改造后/home/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例1/改造后/mysite/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例3/第18节/main/README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例3/第18节/main/home/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例1/改造前/home/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例1/改造后/home/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/home/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/mysite/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例3/第18节/main/mysite/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/home/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/mysite/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/mysite/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/mysite/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例3/第18节/main/home/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/home/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/home/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 极客邦RAG课程配套代码 2 | 3 | 课程网址 4 | https://time.geekbang.org/column/intro/100817901 5 | -------------------------------------------------------------------------------- /实战案例1/改造前/home/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | -------------------------------------------------------------------------------- /实战案例1/改造前/home/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /实战案例1/改造后/home/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /实战案例3/第18节/main/home/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/home/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /实战案例3/第19节/result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weiminye/time-geekbang-org-rag/HEAD/实战案例3/第19节/result.png -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/home/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /实战案例3/第20节/result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weiminye/time-geekbang-org-rag/HEAD/实战案例3/第20节/result.png -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /实战案例3/第21节/result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weiminye/time-geekbang-org-rag/HEAD/实战案例3/第21节/result.png -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /实战案例1/改造前/requirements.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weiminye/time-geekbang-org-rag/HEAD/实战案例1/改造前/requirements.txt -------------------------------------------------------------------------------- /实战案例1/改造前/templates/home/index.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block main_content%} 4 | 5 | {% endblock %} -------------------------------------------------------------------------------- /实战案例1/改造后/requirements.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weiminye/time-geekbang-org-rag/HEAD/实战案例1/改造后/requirements.txt -------------------------------------------------------------------------------- /实战案例3/第19节/要导入的数据/data.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weiminye/time-geekbang-org-rag/HEAD/实战案例3/第19节/要导入的数据/data.xlsx -------------------------------------------------------------------------------- /实战案例3/第21节/要导入的数据/data.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weiminye/time-geekbang-org-rag/HEAD/实战案例3/第21节/要导入的数据/data.xlsx -------------------------------------------------------------------------------- /实战案例3/第18节/main/requirements.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weiminye/time-geekbang-org-rag/HEAD/实战案例3/第18节/main/requirements.txt -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "python.analysis.typeCheckingMode": "basic", 3 | "python.analysis.autoImportCompletions": true 4 | } -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/requirements.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weiminye/time-geekbang-org-rag/HEAD/实战案例2/整合了案例2的实战案例1/requirements.txt -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/requirements.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weiminye/time-geekbang-org-rag/HEAD/实战案例3/第19节/程序/main/requirements.txt -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/requirements.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weiminye/time-geekbang-org-rag/HEAD/实战案例3/第20节/程序/main/requirements.txt -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/requirements.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weiminye/time-geekbang-org-rag/HEAD/实战案例3/第21节/程序/main/requirements.txt -------------------------------------------------------------------------------- /实战案例2/feed.bat: -------------------------------------------------------------------------------- 1 | call F:\miniconda3\Scripts\activate.bat F:\miniconda3 2 | call conda activate rag2 3 | call cd /d F:\temp\rag2\ 4 | call python feed.py 5 | -------------------------------------------------------------------------------- /实战案例1/改造前/home/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class HomeConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'home' 7 | -------------------------------------------------------------------------------- /实战案例1/改造后/home/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class HomeConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'home' 7 | -------------------------------------------------------------------------------- /实战案例3/第18节/main/home/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class HomeConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'home' 7 | -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/home/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class HomeConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'home' 7 | -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/home/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class HomeConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'home' 7 | -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class HomeConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'home' 7 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class HomeConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'home' 7 | -------------------------------------------------------------------------------- /实战案例1/改造前/home/search.py: -------------------------------------------------------------------------------- 1 | from .models import 销售入账记录 2 | 3 | def 查询(查询参数): 4 | if '模块' in 查询参数: 5 | if 查询参数['模块'] == '销售对账': 6 | if '客户名称' in 查询参数: 7 | 客户 = 查询参数['客户名称'].strip() 8 | return 销售入账记录.objects.filter(客户__icontains=客户) -------------------------------------------------------------------------------- /实战案例1/改造后/home/search.py: -------------------------------------------------------------------------------- 1 | from .models import 销售入账记录 2 | 3 | def 查询(查询参数): 4 | if '模块' in 查询参数: 5 | if 查询参数['模块'] == 1: #'销售对账' 6 | if '客户名称' in 查询参数: 7 | 客户 = 查询参数['客户名称'].strip() 8 | return 销售入账记录.objects.filter(客户__icontains=客户) -------------------------------------------------------------------------------- /实战案例1/改造前/home/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path 2 | 3 | from . import views 4 | 5 | app_name = "home" 6 | urlpatterns = [ 7 | path("", views.index, name="index"), 8 | path("salescheck", views.salescheck, name="salescheck"), 9 | path("addsalescheck", views.addsalescheck, name="addsalescheck"), 10 | ] 11 | -------------------------------------------------------------------------------- /实战案例1/改造后/home/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | from .models import 对话记录 4 | 5 | class 对话记录Admin(admin.ModelAdmin): 6 | ordering = ["created_time"] 7 | list_display = ['已结束','created_time','不带入大模型对话中','role', 'content', '处理后content','提交给大模型的playload'] 8 | search_fields = ['已结束'] 9 | list_filter = ('已结束',) 10 | 11 | admin.site.register(对话记录, 对话记录Admin) 12 | -------------------------------------------------------------------------------- /实战案例3/第18节/main/home/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | from .models import 对话记录 4 | 5 | class 对话记录Admin(admin.ModelAdmin): 6 | ordering = ["created_time"] 7 | list_display = ['已结束','created_time','不带入大模型对话中','role', 'content', '处理后content','提交给大模型的playload'] 8 | search_fields = ['已结束'] 9 | list_filter = ('已结束',) 10 | 11 | admin.site.register(对话记录, 对话记录Admin) 12 | -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/home/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | from .models import 对话记录 4 | 5 | class 对话记录Admin(admin.ModelAdmin): 6 | ordering = ["created_time"] 7 | list_display = ['已结束','created_time','不带入大模型对话中','role', 'content', '处理后content','提交给大模型的playload'] 8 | search_fields = ['已结束'] 9 | list_filter = ('已结束',) 10 | 11 | admin.site.register(对话记录, 对话记录Admin) 12 | -------------------------------------------------------------------------------- /实战案例1/改造前/home/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | class 销售入账记录(models.Model): 4 | id = models.IntegerField( 5 | primary_key = True, 6 | editable = False) 7 | 8 | 客户 = models.CharField(max_length=255) 9 | 入账日期 = models.DateTimeField() 10 | 入账金额 = models.TextField(null=True) 11 | 已到账款项 = models.IntegerField(null=True) 12 | 剩余到账款项 = models.IntegerField(null=True) 13 | -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/home/migrations/0012_remove_知识主表_创建时间.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-11-11 10:02 2 | 3 | from django.db import migrations 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0011_知识主表_模块_alter_知识主表_url_alter_知识主表_标题'), 10 | ] 11 | 12 | operations = [ 13 | migrations.RemoveField( 14 | model_name='知识主表', 15 | name='创建时间', 16 | ), 17 | ] 18 | -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | from .models import 对话记录 4 | from import_export.admin import ImportExportModelAdmin 5 | 6 | class 对话记录Admin(ImportExportModelAdmin): 7 | list_display = ['不带入大模型对话中','role', '处理后content','positive_review','negative_review'] 8 | search_fields = ['positive_review','negative_review'] 9 | list_filter = ['positive_review','negative_review'] 10 | 11 | admin.site.register(对话记录, 对话记录Admin) 12 | -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/migrations/0012_remove_知识主表_创建时间.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-11-11 10:02 2 | 3 | from django.db import migrations 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0011_知识主表_模块_alter_知识主表_url_alter_知识主表_标题'), 10 | ] 11 | 12 | operations = [ 13 | migrations.RemoveField( 14 | model_name='知识主表', 15 | name='创建时间', 16 | ), 17 | ] 18 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | from .models import 对话记录 4 | from import_export.admin import ImportExportModelAdmin 5 | 6 | class 对话记录Admin(ImportExportModelAdmin): 7 | list_display = ['不带入大模型对话中','role', '处理后content','positive_review','negative_review'] 8 | search_fields = ['positive_review','negative_review'] 9 | list_filter = ['positive_review','negative_review'] 10 | 11 | admin.site.register(对话记录, 对话记录Admin) 12 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/migrations/0012_remove_知识主表_创建时间.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-11-11 10:02 2 | 3 | from django.db import migrations 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0011_知识主表_模块_alter_知识主表_url_alter_知识主表_标题'), 10 | ] 11 | 12 | operations = [ 13 | migrations.RemoveField( 14 | model_name='知识主表', 15 | name='创建时间', 16 | ), 17 | ] 18 | -------------------------------------------------------------------------------- /实战案例1/改造前/mysite/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for mysite project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /实战案例1/改造前/mysite/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for mysite project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /实战案例1/改造后/mysite/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for mysite project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /实战案例1/改造后/mysite/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for mysite project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/mysite/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for mysite project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/mysite/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for mysite project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /实战案例3/第18节/main/mysite/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for mysite project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /实战案例3/第18节/main/mysite/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for mysite project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/mysite/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for mysite project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/mysite/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for mysite project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/mysite/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for mysite project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/mysite/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for mysite project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/mysite/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for mysite project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/mysite/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for mysite project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /实战案例1/改造后/home/migrations/0003_对话记录_不带入大模型对话中.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-07 02:12 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0002_对话记录'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='对话记录', 15 | name='不带入大模型对话中', 16 | field=models.BooleanField(default=False), 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /实战案例1/改造后/home/migrations/0004_对话记录_已结束.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-07 02:40 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0003_对话记录_不带入大模型对话中'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='对话记录', 15 | name='已结束', 16 | field=models.BooleanField(default=False), 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /实战案例1/改造后/home/migrations/0005_rename_展示content_对话记录_处理后content.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-07 03:16 2 | 3 | from django.db import migrations 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0004_对话记录_已结束'), 10 | ] 11 | 12 | operations = [ 13 | migrations.RenameField( 14 | model_name='对话记录', 15 | old_name='展示content', 16 | new_name='处理后content', 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /实战案例2/将新闻导入数据库.py: -------------------------------------------------------------------------------- 1 | import json 2 | import requests 3 | 4 | if __name__ == "__main__": 5 | # 读取result.json文件, 6 | with open("result.json", "r", encoding="utf-8") as f: 7 | data = json.load(f) 8 | 9 | url = "http://127.0.0.1:8000/api/add-cnet-news" 10 | # 将data做为form-data以post形式发给url 11 | headers = {'Content-Type': "application/json"} 12 | response = requests.post(url, json=data,headers=headers) 13 | 14 | # 打印response的结果 15 | print(response.text) 16 | -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/home/migrations/0004_对话记录_已结束.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-07 02:40 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0003_对话记录_不带入大模型对话中'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='对话记录', 15 | name='已结束', 16 | field=models.BooleanField(default=False), 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /实战案例3/第18节/main/home/migrations/0003_对话记录_不带入大模型对话中.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-07 02:12 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0002_对话记录'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='对话记录', 15 | name='不带入大模型对话中', 16 | field=models.BooleanField(default=False), 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /实战案例3/第18节/main/home/migrations/0004_对话记录_已结束.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-07 02:40 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0003_对话记录_不带入大模型对话中'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='对话记录', 15 | name='已结束', 16 | field=models.BooleanField(default=False), 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/home/migrations/0004_对话记录_已结束.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-07 02:40 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0003_对话记录_不带入大模型对话中'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='对话记录', 15 | name='已结束', 16 | field=models.BooleanField(default=False), 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/migrations/0004_对话记录_已结束.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-07 02:40 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0003_对话记录_不带入大模型对话中'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='对话记录', 15 | name='已结束', 16 | field=models.BooleanField(default=False), 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/migrations/0004_对话记录_已结束.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-07 02:40 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0003_对话记录_不带入大模型对话中'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='对话记录', 15 | name='已结束', 16 | field=models.BooleanField(default=False), 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/home/migrations/0003_对话记录_不带入大模型对话中.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-07 02:12 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0002_对话记录'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='对话记录', 15 | name='不带入大模型对话中', 16 | field=models.BooleanField(default=False), 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/home/migrations/0005_rename_展示content_对话记录_处理后content.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-07 03:16 2 | 3 | from django.db import migrations 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0004_对话记录_已结束'), 10 | ] 11 | 12 | operations = [ 13 | migrations.RenameField( 14 | model_name='对话记录', 15 | old_name='展示content', 16 | new_name='处理后content', 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /实战案例3/第18节/main/home/migrations/0005_rename_展示content_对话记录_处理后content.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-07 03:16 2 | 3 | from django.db import migrations 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0004_对话记录_已结束'), 10 | ] 11 | 12 | operations = [ 13 | migrations.RenameField( 14 | model_name='对话记录', 15 | old_name='展示content', 16 | new_name='处理后content', 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/home/migrations/0003_对话记录_不带入大模型对话中.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-07 02:12 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0002_对话记录'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='对话记录', 15 | name='不带入大模型对话中', 16 | field=models.BooleanField(default=False), 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/home/migrations/0005_rename_展示content_对话记录_处理后content.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-07 03:16 2 | 3 | from django.db import migrations 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0004_对话记录_已结束'), 10 | ] 11 | 12 | operations = [ 13 | migrations.RenameField( 14 | model_name='对话记录', 15 | old_name='展示content', 16 | new_name='处理后content', 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/migrations/0003_对话记录_不带入大模型对话中.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-07 02:12 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0002_对话记录'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='对话记录', 15 | name='不带入大模型对话中', 16 | field=models.BooleanField(default=False), 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/migrations/0005_rename_展示content_对话记录_处理后content.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-07 03:16 2 | 3 | from django.db import migrations 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0004_对话记录_已结束'), 10 | ] 11 | 12 | operations = [ 13 | migrations.RenameField( 14 | model_name='对话记录', 15 | old_name='展示content', 16 | new_name='处理后content', 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/migrations/0003_对话记录_不带入大模型对话中.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-07 02:12 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0002_对话记录'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='对话记录', 15 | name='不带入大模型对话中', 16 | field=models.BooleanField(default=False), 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/migrations/0005_rename_展示content_对话记录_处理后content.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-07 03:16 2 | 3 | from django.db import migrations 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0004_对话记录_已结束'), 10 | ] 11 | 12 | operations = [ 13 | migrations.RenameField( 14 | model_name='对话记录', 15 | old_name='展示content', 16 | new_name='处理后content', 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/home/migrations/0008_alter_cnet新闻_新闻发布日期.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-09-03 02:36 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0007_cnet新闻'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AlterField( 14 | model_name='cnet新闻', 15 | name='新闻发布日期', 16 | field=models.DateTimeField(null=True), 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /实战案例3/嵌入模型服务/download.py: -------------------------------------------------------------------------------- 1 | from langchain.embeddings import HuggingFaceBgeEmbeddings 2 | 3 | 向量编码模型本地存放路径 = "F:\LLAMA_INDEX_CACHE_DIR\models\embeddings" 4 | 向量编码本地所使用的device = "cpu" # "cuda" 5 | 6 | model_name = "BAAI/bge-large-zh-v1.5" 7 | cache_folder=向量编码模型本地存放路径 8 | model_kwargs = {'device': 向量编码本地所使用的device} #{'device': 'cuda'} # {'device': 'cpu'} 9 | encode_kwargs = {'normalize_embeddings': True} # set True to compute cosine similarity 10 | HuggingFaceBgeEmbeddings(model_name=model_name,cache_folder=cache_folder,model_kwargs=model_kwargs,encode_kwargs=encode_kwargs) -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/migrations/0016_remove_关键词_created_time_remove_关键词_lastmodified_time.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-11-25 18:08 2 | 3 | from django.db import migrations 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0015_关键词'), 10 | ] 11 | 12 | operations = [ 13 | migrations.RemoveField( 14 | model_name='关键词', 15 | name='created_time', 16 | ), 17 | migrations.RemoveField( 18 | model_name='关键词', 19 | name='lastmodified_time', 20 | ), 21 | ] 22 | -------------------------------------------------------------------------------- /实战案例3/第19节/要导入的数据/import_data_from_excel.py: -------------------------------------------------------------------------------- 1 | # 读取当前目录下的data.xlsx文件 2 | import pandas as pd 3 | import requests 4 | 5 | data = pd.read_excel('data.xlsx') 6 | 7 | # 遍历数据框中的每一行数据 8 | for index, row in data.iterrows(): 9 | body_json = { 10 | "知识主表id":row['知识主表id'], 11 | "知识详细表记录id":row['知识详细表记录id'], 12 | "模块":row['模块'], 13 | "标题":row['标题'], 14 | "url":row['url'], 15 | "文本内容":row['文本内容'], 16 | "内容在分段中的顺序":row['内容在分段中的顺序'], 17 | } 18 | response = requests.post(f"http://127.0.0.1:8000/api/import-data", json=body_json) 19 | print("已成功导入:" + response.text) 20 | -------------------------------------------------------------------------------- /实战案例3/第21节/要导入的数据/import_data_from_excel.py: -------------------------------------------------------------------------------- 1 | # 读取当前目录下的data.xlsx文件 2 | import pandas as pd 3 | import requests 4 | 5 | data = pd.read_excel('data.xlsx') 6 | 7 | # 遍历数据框中的每一行数据 8 | for index, row in data.iterrows(): 9 | body_json = { 10 | "知识主表id":row['知识主表id'], 11 | "知识详细表记录id":row['知识详细表记录id'], 12 | "模块":row['模块'], 13 | "标题":row['标题'], 14 | "url":row['url'], 15 | "文本内容":row['文本内容'], 16 | "内容在分段中的顺序":row['内容在分段中的顺序'], 17 | } 18 | response = requests.post(f"http://127.0.0.1:8000/api/import-data", json=body_json) 19 | print("已成功导入:" + response.text) 20 | -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/home/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | from .models import 对话记录 4 | from .models import CNET新闻 5 | 6 | class 对话记录Admin(admin.ModelAdmin): 7 | ordering = ["created_time"] 8 | list_display = ['已结束','created_time','不带入大模型对话中','role', 'content', '处理后content','提交给大模型的playload'] 9 | search_fields = ['已结束'] 10 | list_filter = ['已结束'] 11 | 12 | admin.site.register(对话记录, 对话记录Admin) 13 | 14 | class CNET新闻Admin(admin.ModelAdmin): 15 | ordering = ["新闻发布日期"] 16 | list_display = ['标题中文翻译','新闻发布日期','url'] 17 | list_filter = ['新闻发布日期'] 18 | 19 | admin.site.register(CNET新闻, CNET新闻Admin) -------------------------------------------------------------------------------- /实战案例3/嵌入模型服务/main.py: -------------------------------------------------------------------------------- 1 | from fastapi import FastAPI, Request 2 | import uvicorn 3 | from 向量编码器 import 向量编码器 4 | 5 | app = FastAPI() 6 | 7 | @app.get("/") 8 | async def read_root(): 9 | return {"hello word":"这里是向量编码服务"} 10 | 11 | app.向量编码器instance = None 12 | 13 | @app.on_event('startup') 14 | def init_data(): 15 | app.向量编码器instance = 向量编码器() 16 | 17 | @app.post("/api/embedding/encode") 18 | async def 向量编码(request: Request): 19 | data = await request.json() 20 | print(data) 21 | input_str= data["input"] 22 | return_result = app.向量编码器instance.向量编码(input_str) 23 | return return_result 24 | # return data 25 | 26 | if __name__ == "__main__": 27 | uvicorn.run("main:app", host="0.0.0.0", port=8902) -------------------------------------------------------------------------------- /实战案例3/第18节/main/home/api.py: -------------------------------------------------------------------------------- 1 | import json 2 | from django.http import JsonResponse 3 | import requests 4 | 5 | from .models import 销售入账记录 6 | def 调用向量编码服务(输入字符串): 7 | body_json = { 8 | "input":输入字符串 9 | } 10 | response = requests.post(f"http://127.0.0.1:8902/api/embedding/encode", json=body_json) 11 | if response.status_code == 200: 12 | return json.loads(response.text)['向量编码'] 13 | else: 14 | raise Exception('失败') 15 | 16 | def 对知识批量进行向量编码(request): 17 | 未编码的知识list = 销售入账记录.objects.filter(客户向量编码__isnull=True) 18 | for current in 未编码的知识list: 19 | current.客户向量编码= 调用向量编码服务(current.客户) 20 | current.save() 21 | 22 | result = {'code':200} 23 | return JsonResponse(result) -------------------------------------------------------------------------------- /实战案例3/第18节/main/home/migrations/0008_alter_对话记录_id_alter_销售入账记录_id.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-10-05 06:42 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0007_销售入账记录_客户向量编码_销售入账记录_客户向量编码模型'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AlterField( 14 | model_name='对话记录', 15 | name='id', 16 | field=models.AutoField(primary_key=True, serialize=False), 17 | ), 18 | migrations.AlterField( 19 | model_name='销售入账记录', 20 | name='id', 21 | field=models.AutoField(primary_key=True, serialize=False), 22 | ), 23 | ] 24 | -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/home/migrations/0008_alter_对话记录_id_alter_销售入账记录_id.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-10-05 06:42 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0007_销售入账记录_客户向量编码_销售入账记录_客户向量编码模型'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AlterField( 14 | model_name='对话记录', 15 | name='id', 16 | field=models.AutoField(primary_key=True, serialize=False), 17 | ), 18 | migrations.AlterField( 19 | model_name='销售入账记录', 20 | name='id', 21 | field=models.AutoField(primary_key=True, serialize=False), 22 | ), 23 | ] 24 | -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/migrations/0008_alter_对话记录_id_alter_销售入账记录_id.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-10-05 06:42 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0007_销售入账记录_客户向量编码_销售入账记录_客户向量编码模型'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AlterField( 14 | model_name='对话记录', 15 | name='id', 16 | field=models.AutoField(primary_key=True, serialize=False), 17 | ), 18 | migrations.AlterField( 19 | model_name='销售入账记录', 20 | name='id', 21 | field=models.AutoField(primary_key=True, serialize=False), 22 | ), 23 | ] 24 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/migrations/0008_alter_对话记录_id_alter_销售入账记录_id.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-10-05 06:42 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0007_销售入账记录_客户向量编码_销售入账记录_客户向量编码模型'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AlterField( 14 | model_name='对话记录', 15 | name='id', 16 | field=models.AutoField(primary_key=True, serialize=False), 17 | ), 18 | migrations.AlterField( 19 | model_name='销售入账记录', 20 | name='id', 21 | field=models.AutoField(primary_key=True, serialize=False), 22 | ), 23 | ] 24 | -------------------------------------------------------------------------------- /实战案例1/改造前/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /实战案例1/改造后/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /实战案例3/第18节/main/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/migrations/0014_对话记录_negative_review_对话记录_positive_review.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-11-17 02:43 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0013_remove_知识主表_created_time_and_more'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='对话记录', 15 | name='negative_review', 16 | field=models.BooleanField(default=False, verbose_name='差评'), 17 | ), 18 | migrations.AddField( 19 | model_name='对话记录', 20 | name='positive_review', 21 | field=models.BooleanField(default=False, verbose_name='好评'), 22 | ), 23 | ] 24 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/migrations/0014_对话记录_negative_review_对话记录_positive_review.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-11-17 02:43 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0013_remove_知识主表_created_time_and_more'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='对话记录', 15 | name='negative_review', 16 | field=models.BooleanField(default=False, verbose_name='差评'), 17 | ), 18 | migrations.AddField( 19 | model_name='对话记录', 20 | name='positive_review', 21 | field=models.BooleanField(default=False, verbose_name='好评'), 22 | ), 23 | ] 24 | -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/home/migrations/0010_知识详细表_内容在分段中的顺序_alter_知识详细表_知识主表.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-11-11 07:38 2 | 3 | from django.db import migrations, models 4 | import django.db.models.deletion 5 | 6 | 7 | class Migration(migrations.Migration): 8 | 9 | dependencies = [ 10 | ('home', '0009_知识主表_知识详细表'), 11 | ] 12 | 13 | operations = [ 14 | migrations.AddField( 15 | model_name='知识详细表', 16 | name='内容在分段中的顺序', 17 | field=models.IntegerField(blank=True, null=True), 18 | ), 19 | migrations.AlterField( 20 | model_name='知识详细表', 21 | name='知识主表', 22 | field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='home.知识主表'), 23 | ), 24 | ] 25 | -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/migrations/0010_知识详细表_内容在分段中的顺序_alter_知识详细表_知识主表.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-11-11 07:38 2 | 3 | from django.db import migrations, models 4 | import django.db.models.deletion 5 | 6 | 7 | class Migration(migrations.Migration): 8 | 9 | dependencies = [ 10 | ('home', '0009_知识主表_知识详细表'), 11 | ] 12 | 13 | operations = [ 14 | migrations.AddField( 15 | model_name='知识详细表', 16 | name='内容在分段中的顺序', 17 | field=models.IntegerField(blank=True, null=True), 18 | ), 19 | migrations.AlterField( 20 | model_name='知识详细表', 21 | name='知识主表', 22 | field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='home.知识主表'), 23 | ), 24 | ] 25 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/migrations/0010_知识详细表_内容在分段中的顺序_alter_知识详细表_知识主表.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-11-11 07:38 2 | 3 | from django.db import migrations, models 4 | import django.db.models.deletion 5 | 6 | 7 | class Migration(migrations.Migration): 8 | 9 | dependencies = [ 10 | ('home', '0009_知识主表_知识详细表'), 11 | ] 12 | 13 | operations = [ 14 | migrations.AddField( 15 | model_name='知识详细表', 16 | name='内容在分段中的顺序', 17 | field=models.IntegerField(blank=True, null=True), 18 | ), 19 | migrations.AlterField( 20 | model_name='知识详细表', 21 | name='知识主表', 22 | field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='home.知识主表'), 23 | ), 24 | ] 25 | -------------------------------------------------------------------------------- /实战案例3/第18节/main/home/migrations/0007_销售入账记录_客户向量编码_销售入账记录_客户向量编码模型.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-10-04 09:19 2 | 3 | from django.db import migrations, models 4 | import pgvector.django.vector 5 | 6 | 7 | class Migration(migrations.Migration): 8 | 9 | dependencies = [ 10 | ('home', '0006_对话记录_提交给大模型的playload_alter_对话记录_content_and_more'), 11 | ] 12 | 13 | operations = [ 14 | migrations.AddField( 15 | model_name='销售入账记录', 16 | name='客户向量编码', 17 | field=pgvector.django.vector.VectorField(blank=True, dimensions=1024, null=True), 18 | ), 19 | migrations.AddField( 20 | model_name='销售入账记录', 21 | name='客户向量编码模型', 22 | field=models.TextField(default='bge-large-zh-v1.5'), 23 | ), 24 | ] 25 | -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/home/migrations/0007_销售入账记录_客户向量编码_销售入账记录_客户向量编码模型.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-10-04 09:19 2 | 3 | from django.db import migrations, models 4 | import pgvector.django.vector 5 | 6 | 7 | class Migration(migrations.Migration): 8 | 9 | dependencies = [ 10 | ('home', '0006_对话记录_提交给大模型的playload_alter_对话记录_content_and_more'), 11 | ] 12 | 13 | operations = [ 14 | migrations.AddField( 15 | model_name='销售入账记录', 16 | name='客户向量编码', 17 | field=pgvector.django.vector.VectorField(blank=True, dimensions=1024, null=True), 18 | ), 19 | migrations.AddField( 20 | model_name='销售入账记录', 21 | name='客户向量编码模型', 22 | field=models.TextField(default='bge-large-zh-v1.5'), 23 | ), 24 | ] 25 | -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/migrations/0007_销售入账记录_客户向量编码_销售入账记录_客户向量编码模型.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-10-04 09:19 2 | 3 | from django.db import migrations, models 4 | import pgvector.django.vector 5 | 6 | 7 | class Migration(migrations.Migration): 8 | 9 | dependencies = [ 10 | ('home', '0006_对话记录_提交给大模型的playload_alter_对话记录_content_and_more'), 11 | ] 12 | 13 | operations = [ 14 | migrations.AddField( 15 | model_name='销售入账记录', 16 | name='客户向量编码', 17 | field=pgvector.django.vector.VectorField(blank=True, dimensions=1024, null=True), 18 | ), 19 | migrations.AddField( 20 | model_name='销售入账记录', 21 | name='客户向量编码模型', 22 | field=models.TextField(default='bge-large-zh-v1.5'), 23 | ), 24 | ] 25 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/migrations/0007_销售入账记录_客户向量编码_销售入账记录_客户向量编码模型.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-10-04 09:19 2 | 3 | from django.db import migrations, models 4 | import pgvector.django.vector 5 | 6 | 7 | class Migration(migrations.Migration): 8 | 9 | dependencies = [ 10 | ('home', '0006_对话记录_提交给大模型的playload_alter_对话记录_content_and_more'), 11 | ] 12 | 13 | operations = [ 14 | migrations.AddField( 15 | model_name='销售入账记录', 16 | name='客户向量编码', 17 | field=pgvector.django.vector.VectorField(blank=True, dimensions=1024, null=True), 18 | ), 19 | migrations.AddField( 20 | model_name='销售入账记录', 21 | name='客户向量编码模型', 22 | field=models.TextField(default='bge-large-zh-v1.5'), 23 | ), 24 | ] 25 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/prompt.py: -------------------------------------------------------------------------------- 1 | def 构造解析用户输入并返回结构化数据用的messages(之前的用户输入,用户输入,module): 2 | if 之前的用户输入 is not None and len(之前的用户输入.strip()) > 0: 3 | 用户输入 = 之前的用户输入 + 用户输入 4 | 5 | if module == 1: 6 | messages=[ 7 | {"role": "user", "content": f""" 8 | 请根据用户的输入返回json格式结果,除此之外不要返回其他内容。 9 | 10 | 示例1: 11 | 用户:客户北京极客邦有限公司的款项到账了多少? 12 | 系统: 13 | {{'模块':1,'客户名称':'北京极客邦有限公司'}} 14 | 15 | 用户:{用户输入} 16 | 系统: 17 | """}, 18 | ] 19 | return messages 20 | # if module == 2: #依样画葫芦处理其他模块 21 | # messages=[ 22 | # {"role": "user", "content": f""" 23 | # 请根据用户的输入返回json格式结果,除此之外不要返回其他内容。 24 | # 25 | # 示例1: 26 | # 用户:客户北京极客邦有限公司的款项到账了多少? 27 | # 系统: 28 | # {{'模块':1,'客户名称':'北京极客邦有限公司'}} 29 | # 30 | # 用户:{用户输入} 31 | # 系统: 32 | # """}, 33 | # ] 34 | return None -------------------------------------------------------------------------------- /实战案例1/改造后/home/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path 2 | 3 | from . import views,views_api 4 | 5 | app_name = "home" 6 | urlpatterns = [ 7 | path("", views.index, name="index"), 8 | path("salescheck", views.salescheck, name="salescheck"), 9 | path("addsalescheck", views.addsalescheck, name="addsalescheck"), 10 | path("newtalk", views.newtalk, name="newtalk"), 11 | 12 | path("api/new-talk", views_api.开始新的对话api, name="api-new-talk"), 13 | path("api/get-query-paras", views_api.获取结构化数据查询参数api, name="api-get-query-paras"), 14 | path("api/answer-without-data", views_api.从数据库查不到相关数据时的操作api, name="api-answer-without-data"), 15 | path("api/answer-with-data", views_api.根据查询结果回答用户输入api, name="api-answer-with-data"), 16 | path("api/get-conversation-list", views_api.获取对话记录api, name="api-get-conversation-list"), 17 | ] 18 | -------------------------------------------------------------------------------- /实战案例3/嵌入模型服务/向量编码器.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | from langchain.embeddings import HuggingFaceBgeEmbeddings 3 | 4 | class 向量编码器: 5 | def __init__(self): 6 | 向量编码模型本地存放路径 = "F:\LLAMA_INDEX_CACHE_DIR\models\embeddings" 7 | 向量编码本地所使用的device = "cpu" 8 | 9 | model_name = "BAAI/bge-large-zh-v1.5" 10 | cache_folder=向量编码模型本地存放路径 11 | model_kwargs = {'device': 向量编码本地所使用的device} #{'device': 'cuda'} # {'device': 'cpu'} 12 | encode_kwargs = {'normalize_embeddings': True} # set True to compute cosine similarity 13 | self.embed_model = HuggingFaceBgeEmbeddings(model_name=model_name,cache_folder=cache_folder,model_kwargs=model_kwargs,encode_kwargs=encode_kwargs) 14 | 15 | def 向量编码(self,query: str): 16 | embed_result = self.embed_model.embed_query(query) 17 | return {'向量编码':embed_result} -------------------------------------------------------------------------------- /实战案例1/改造后/home/migrations/0002_对话记录.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-06 09:01 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0001_initial'), 10 | ] 11 | 12 | operations = [ 13 | migrations.CreateModel( 14 | name='对话记录', 15 | fields=[ 16 | ('id', models.IntegerField(editable=False, primary_key=True, serialize=False)), 17 | ('role', models.TextField()), 18 | ('展示content', models.TextField()), 19 | ('content', models.TextField()), 20 | ('created_time', models.DateTimeField(auto_now_add=True)), 21 | ('lastmodified_time', models.DateTimeField(auto_now=True)), 22 | ], 23 | ), 24 | ] 25 | -------------------------------------------------------------------------------- /实战案例3/第18节/main/home/migrations/0002_对话记录.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-06 09:01 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0001_initial'), 10 | ] 11 | 12 | operations = [ 13 | migrations.CreateModel( 14 | name='对话记录', 15 | fields=[ 16 | ('id', models.IntegerField(editable=False, primary_key=True, serialize=False)), 17 | ('role', models.TextField()), 18 | ('展示content', models.TextField()), 19 | ('content', models.TextField()), 20 | ('created_time', models.DateTimeField(auto_now_add=True)), 21 | ('lastmodified_time', models.DateTimeField(auto_now=True)), 22 | ], 23 | ), 24 | ] 25 | -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/home/migrations/0002_对话记录.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-06 09:01 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0001_initial'), 10 | ] 11 | 12 | operations = [ 13 | migrations.CreateModel( 14 | name='对话记录', 15 | fields=[ 16 | ('id', models.IntegerField(editable=False, primary_key=True, serialize=False)), 17 | ('role', models.TextField()), 18 | ('展示content', models.TextField()), 19 | ('content', models.TextField()), 20 | ('created_time', models.DateTimeField(auto_now_add=True)), 21 | ('lastmodified_time', models.DateTimeField(auto_now=True)), 22 | ], 23 | ), 24 | ] 25 | -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/home/migrations/0002_对话记录.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-06 09:01 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0001_initial'), 10 | ] 11 | 12 | operations = [ 13 | migrations.CreateModel( 14 | name='对话记录', 15 | fields=[ 16 | ('id', models.IntegerField(editable=False, primary_key=True, serialize=False)), 17 | ('role', models.TextField()), 18 | ('展示content', models.TextField()), 19 | ('content', models.TextField()), 20 | ('created_time', models.DateTimeField(auto_now_add=True)), 21 | ('lastmodified_time', models.DateTimeField(auto_now=True)), 22 | ], 23 | ), 24 | ] 25 | -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/migrations/0002_对话记录.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-06 09:01 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0001_initial'), 10 | ] 11 | 12 | operations = [ 13 | migrations.CreateModel( 14 | name='对话记录', 15 | fields=[ 16 | ('id', models.IntegerField(editable=False, primary_key=True, serialize=False)), 17 | ('role', models.TextField()), 18 | ('展示content', models.TextField()), 19 | ('content', models.TextField()), 20 | ('created_time', models.DateTimeField(auto_now_add=True)), 21 | ('lastmodified_time', models.DateTimeField(auto_now=True)), 22 | ], 23 | ), 24 | ] 25 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/migrations/0002_对话记录.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-06 09:01 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0001_initial'), 10 | ] 11 | 12 | operations = [ 13 | migrations.CreateModel( 14 | name='对话记录', 15 | fields=[ 16 | ('id', models.IntegerField(editable=False, primary_key=True, serialize=False)), 17 | ('role', models.TextField()), 18 | ('展示content', models.TextField()), 19 | ('content', models.TextField()), 20 | ('created_time', models.DateTimeField(auto_now_add=True)), 21 | ('lastmodified_time', models.DateTimeField(auto_now=True)), 22 | ], 23 | ), 24 | ] 25 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/migrations/0015_关键词.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-11-25 17:56 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0014_对话记录_negative_review_对话记录_positive_review'), 10 | ] 11 | 12 | operations = [ 13 | migrations.CreateModel( 14 | name='关键词', 15 | fields=[ 16 | ('id', models.IntegerField(editable=False, primary_key=True, serialize=False)), 17 | ('created_time', models.DateTimeField(auto_now_add=True)), 18 | ('lastmodified_time', models.DateTimeField(auto_now=True)), 19 | ('关键词', models.TextField()), 20 | ('模块', models.TextField()), 21 | ('备注', models.TextField()), 22 | ], 23 | ), 24 | ] 25 | -------------------------------------------------------------------------------- /实战案例1/改造后/home/migrations/0006_对话记录_提交给大模型的playload_alter_对话记录_content_and_more.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-07 06:15 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0005_rename_展示content_对话记录_处理后content'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='对话记录', 15 | name='提交给大模型的playload', 16 | field=models.TextField(null=True), 17 | ), 18 | migrations.AlterField( 19 | model_name='对话记录', 20 | name='content', 21 | field=models.TextField(null=True), 22 | ), 23 | migrations.AlterField( 24 | model_name='对话记录', 25 | name='处理后content', 26 | field=models.TextField(null=True), 27 | ), 28 | ] 29 | -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/home/search.py: -------------------------------------------------------------------------------- 1 | from datetime import datetime 2 | from .models import CNET新闻, 销售入账记录 3 | 4 | def 查询(查询参数): 5 | if '模块' in 查询参数: 6 | if 查询参数['模块'] == 1: #'销售对账' 7 | if '客户名称' in 查询参数: 8 | 客户 = 查询参数['客户名称'].strip() 9 | return 销售入账记录.objects.filter(客户__icontains=客户) 10 | if 查询参数['模块'] == 6: #'CNET新闻' 11 | if '日期' in 查询参数: 12 | 日期 = 查询参数['日期'].strip() 13 | print(f'日期={日期}') 14 | # 定义日期字符串 15 | date_str = '2024-09-14' 16 | 17 | # 定义日期格式 18 | date_format = '%Y-%m-%d' 19 | 20 | # 将字符串转换为日期对象 21 | date_obj = datetime.strptime(date_str, date_format) 22 | 23 | 查询结果 = CNET新闻.objects.filter(新闻发布日期__date=date_obj).values_list('标题中文翻译', flat=True) 24 | return 查询结果 -------------------------------------------------------------------------------- /实战案例3/第18节/main/home/migrations/0006_对话记录_提交给大模型的playload_alter_对话记录_content_and_more.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-07 06:15 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0005_rename_展示content_对话记录_处理后content'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='对话记录', 15 | name='提交给大模型的playload', 16 | field=models.TextField(null=True), 17 | ), 18 | migrations.AlterField( 19 | model_name='对话记录', 20 | name='content', 21 | field=models.TextField(null=True), 22 | ), 23 | migrations.AlterField( 24 | model_name='对话记录', 25 | name='处理后content', 26 | field=models.TextField(null=True), 27 | ), 28 | ] 29 | -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/home/migrations/0006_对话记录_提交给大模型的playload_alter_对话记录_content_and_more.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-07 06:15 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0005_rename_展示content_对话记录_处理后content'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='对话记录', 15 | name='提交给大模型的playload', 16 | field=models.TextField(null=True), 17 | ), 18 | migrations.AlterField( 19 | model_name='对话记录', 20 | name='content', 21 | field=models.TextField(null=True), 22 | ), 23 | migrations.AlterField( 24 | model_name='对话记录', 25 | name='处理后content', 26 | field=models.TextField(null=True), 27 | ), 28 | ] 29 | -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/home/migrations/0006_对话记录_提交给大模型的playload_alter_对话记录_content_and_more.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-07 06:15 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0005_rename_展示content_对话记录_处理后content'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='对话记录', 15 | name='提交给大模型的playload', 16 | field=models.TextField(null=True), 17 | ), 18 | migrations.AlterField( 19 | model_name='对话记录', 20 | name='content', 21 | field=models.TextField(null=True), 22 | ), 23 | migrations.AlterField( 24 | model_name='对话记录', 25 | name='处理后content', 26 | field=models.TextField(null=True), 27 | ), 28 | ] 29 | -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/home/migrations/0011_知识主表_模块_alter_知识主表_url_alter_知识主表_标题.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-11-11 08:52 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0010_知识详细表_内容在分段中的顺序_alter_知识详细表_知识主表'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='知识主表', 15 | name='模块', 16 | field=models.TextField(blank=True, null=True), 17 | ), 18 | migrations.AlterField( 19 | model_name='知识主表', 20 | name='url', 21 | field=models.TextField(blank=True, null=True), 22 | ), 23 | migrations.AlterField( 24 | model_name='知识主表', 25 | name='标题', 26 | field=models.TextField(blank=True, null=True), 27 | ), 28 | ] 29 | -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/migrations/0006_对话记录_提交给大模型的playload_alter_对话记录_content_and_more.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-07 06:15 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0005_rename_展示content_对话记录_处理后content'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='对话记录', 15 | name='提交给大模型的playload', 16 | field=models.TextField(null=True), 17 | ), 18 | migrations.AlterField( 19 | model_name='对话记录', 20 | name='content', 21 | field=models.TextField(null=True), 22 | ), 23 | migrations.AlterField( 24 | model_name='对话记录', 25 | name='处理后content', 26 | field=models.TextField(null=True), 27 | ), 28 | ] 29 | -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/migrations/0011_知识主表_模块_alter_知识主表_url_alter_知识主表_标题.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-11-11 08:52 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0010_知识详细表_内容在分段中的顺序_alter_知识详细表_知识主表'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='知识主表', 15 | name='模块', 16 | field=models.TextField(blank=True, null=True), 17 | ), 18 | migrations.AlterField( 19 | model_name='知识主表', 20 | name='url', 21 | field=models.TextField(blank=True, null=True), 22 | ), 23 | migrations.AlterField( 24 | model_name='知识主表', 25 | name='标题', 26 | field=models.TextField(blank=True, null=True), 27 | ), 28 | ] 29 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/migrations/0006_对话记录_提交给大模型的playload_alter_对话记录_content_and_more.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-07-07 06:15 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0005_rename_展示content_对话记录_处理后content'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='对话记录', 15 | name='提交给大模型的playload', 16 | field=models.TextField(null=True), 17 | ), 18 | migrations.AlterField( 19 | model_name='对话记录', 20 | name='content', 21 | field=models.TextField(null=True), 22 | ), 23 | migrations.AlterField( 24 | model_name='对话记录', 25 | name='处理后content', 26 | field=models.TextField(null=True), 27 | ), 28 | ] 29 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/migrations/0011_知识主表_模块_alter_知识主表_url_alter_知识主表_标题.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-11-11 08:52 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0010_知识详细表_内容在分段中的顺序_alter_知识详细表_知识主表'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='知识主表', 15 | name='模块', 16 | field=models.TextField(blank=True, null=True), 17 | ), 18 | migrations.AlterField( 19 | model_name='知识主表', 20 | name='url', 21 | field=models.TextField(blank=True, null=True), 22 | ), 23 | migrations.AlterField( 24 | model_name='知识主表', 25 | name='标题', 26 | field=models.TextField(blank=True, null=True), 27 | ), 28 | ] 29 | -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/home/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path 2 | 3 | from . import views,views_api 4 | 5 | app_name = "home" 6 | urlpatterns = [ 7 | path("", views.index, name="index"), 8 | path("salescheck", views.salescheck, name="salescheck"), 9 | path("addsalescheck", views.addsalescheck, name="addsalescheck"), 10 | path("newtalk", views.newtalk, name="newtalk"), 11 | 12 | path("api/new-talk", views_api.开始新的对话api, name="api-new-talk"), 13 | path("api/get-query-paras", views_api.获取结构化数据查询参数api, name="api-get-query-paras"), 14 | path("api/answer-without-data", views_api.从数据库查不到相关数据时的操作api, name="api-answer-without-data"), 15 | path("api/answer-with-data", views_api.根据查询结果回答用户输入api, name="api-answer-with-data"), 16 | path("api/get-conversation-list", views_api.获取对话记录api, name="api-get-conversation-list"), 17 | path("api/add-cnet-news", views_api.CNET新闻入库api, name="add-cnet-news"), 18 | ] 19 | -------------------------------------------------------------------------------- /实战案例1/改造前/mysite/urls.py: -------------------------------------------------------------------------------- 1 | """ 2 | URL configuration for mysite project. 3 | 4 | The `urlpatterns` list routes URLs to views. For more information please see: 5 | https://docs.djangoproject.com/en/4.2/topics/http/urls/ 6 | Examples: 7 | Function views 8 | 1. Add an import: from my_app import views 9 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 10 | Class-based views 11 | 1. Add an import: from other_app.views import Home 12 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 13 | Including another URLconf 14 | 1. Import the include() function: from django.urls import include, path 15 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 16 | """ 17 | from django.contrib import admin 18 | from django.urls import include, path 19 | 20 | app_name = "home" 21 | urlpatterns = [ 22 | path('', include('home.urls')), 23 | # path('admin/', admin.site.urls), 24 | ] 25 | -------------------------------------------------------------------------------- /实战案例1/改造后/mysite/urls.py: -------------------------------------------------------------------------------- 1 | """ 2 | URL configuration for mysite project. 3 | 4 | The `urlpatterns` list routes URLs to views. For more information please see: 5 | https://docs.djangoproject.com/en/4.2/topics/http/urls/ 6 | Examples: 7 | Function views 8 | 1. Add an import: from my_app import views 9 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 10 | Class-based views 11 | 1. Add an import: from other_app.views import Home 12 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 13 | Including another URLconf 14 | 1. Import the include() function: from django.urls import include, path 15 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 16 | """ 17 | from django.contrib import admin 18 | from django.urls import include, path 19 | 20 | app_name = "home" 21 | urlpatterns = [ 22 | path('', include('home.urls')), 23 | path('admin/', admin.site.urls), 24 | ] 25 | -------------------------------------------------------------------------------- /实战案例3/第18节/main/home/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path 2 | 3 | from . import views,views_api,api 4 | 5 | app_name = "home" 6 | urlpatterns = [ 7 | path("", views.index, name="index"), 8 | path("salescheck", views.salescheck, name="salescheck"), 9 | path("addsalescheck", views.addsalescheck, name="addsalescheck"), 10 | path("newtalk", views.newtalk, name="newtalk"), 11 | 12 | path("api/new-talk", views_api.开始新的对话api, name="api-new-talk"), 13 | path("api/get-query-paras", views_api.获取结构化数据查询参数api, name="api-get-query-paras"), 14 | path("api/answer-without-data", views_api.从数据库查不到相关数据时的操作api, name="api-answer-without-data"), 15 | path("api/answer-with-data", views_api.根据查询结果回答用户输入api, name="api-answer-with-data"), 16 | path("api/get-conversation-list", views_api.获取对话记录api, name="api-get-conversation-list"), 17 | path("api/knowledge-embedding-batch", api.对知识批量进行向量编码, name="knowledge-embedding-batch"), 18 | ] 19 | -------------------------------------------------------------------------------- /实战案例3/第18节/main/mysite/urls.py: -------------------------------------------------------------------------------- 1 | """ 2 | URL configuration for mysite project. 3 | 4 | The `urlpatterns` list routes URLs to views. For more information please see: 5 | https://docs.djangoproject.com/en/4.2/topics/http/urls/ 6 | Examples: 7 | Function views 8 | 1. Add an import: from my_app import views 9 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 10 | Class-based views 11 | 1. Add an import: from other_app.views import Home 12 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 13 | Including another URLconf 14 | 1. Import the include() function: from django.urls import include, path 15 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 16 | """ 17 | from django.contrib import admin 18 | from django.urls import include, path 19 | 20 | app_name = "home" 21 | urlpatterns = [ 22 | path('', include('home.urls')), 23 | path('admin/', admin.site.urls), 24 | ] 25 | -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/mysite/urls.py: -------------------------------------------------------------------------------- 1 | """ 2 | URL configuration for mysite project. 3 | 4 | The `urlpatterns` list routes URLs to views. For more information please see: 5 | https://docs.djangoproject.com/en/4.2/topics/http/urls/ 6 | Examples: 7 | Function views 8 | 1. Add an import: from my_app import views 9 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 10 | Class-based views 11 | 1. Add an import: from other_app.views import Home 12 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 13 | Including another URLconf 14 | 1. Import the include() function: from django.urls import include, path 15 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 16 | """ 17 | from django.contrib import admin 18 | from django.urls import include, path 19 | 20 | app_name = "home" 21 | urlpatterns = [ 22 | path('', include('home.urls')), 23 | path('admin/', admin.site.urls), 24 | ] 25 | -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/mysite/urls.py: -------------------------------------------------------------------------------- 1 | """ 2 | URL configuration for mysite project. 3 | 4 | The `urlpatterns` list routes URLs to views. For more information please see: 5 | https://docs.djangoproject.com/en/4.2/topics/http/urls/ 6 | Examples: 7 | Function views 8 | 1. Add an import: from my_app import views 9 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 10 | Class-based views 11 | 1. Add an import: from other_app.views import Home 12 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 13 | Including another URLconf 14 | 1. Import the include() function: from django.urls import include, path 15 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 16 | """ 17 | from django.contrib import admin 18 | from django.urls import include, path 19 | 20 | app_name = "home" 21 | urlpatterns = [ 22 | path('', include('home.urls')), 23 | path('admin/', admin.site.urls), 24 | ] 25 | -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/mysite/urls.py: -------------------------------------------------------------------------------- 1 | """ 2 | URL configuration for mysite project. 3 | 4 | The `urlpatterns` list routes URLs to views. For more information please see: 5 | https://docs.djangoproject.com/en/4.2/topics/http/urls/ 6 | Examples: 7 | Function views 8 | 1. Add an import: from my_app import views 9 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 10 | Class-based views 11 | 1. Add an import: from other_app.views import Home 12 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 13 | Including another URLconf 14 | 1. Import the include() function: from django.urls import include, path 15 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 16 | """ 17 | from django.contrib import admin 18 | from django.urls import include, path 19 | 20 | app_name = "home" 21 | urlpatterns = [ 22 | path('', include('home.urls')), 23 | path('admin/', admin.site.urls), 24 | ] 25 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/mysite/urls.py: -------------------------------------------------------------------------------- 1 | """ 2 | URL configuration for mysite project. 3 | 4 | The `urlpatterns` list routes URLs to views. For more information please see: 5 | https://docs.djangoproject.com/en/4.2/topics/http/urls/ 6 | Examples: 7 | Function views 8 | 1. Add an import: from my_app import views 9 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 10 | Class-based views 11 | 1. Add an import: from other_app.views import Home 12 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 13 | Including another URLconf 14 | 1. Import the include() function: from django.urls import include, path 15 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 16 | """ 17 | from django.contrib import admin 18 | from django.urls import include, path 19 | 20 | app_name = "home" 21 | urlpatterns = [ 22 | path('', include('home.urls')), 23 | path('admin/', admin.site.urls), 24 | ] 25 | -------------------------------------------------------------------------------- /实战案例1/改造后/home/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | class 销售入账记录(models.Model): 4 | id = models.IntegerField( 5 | primary_key = True, 6 | editable = False) 7 | 8 | 客户 = models.CharField(max_length=255) 9 | 入账日期 = models.DateTimeField() 10 | 入账金额 = models.TextField(null=True) 11 | 已到账款项 = models.IntegerField(null=True) 12 | 剩余到账款项 = models.IntegerField(null=True) 13 | 14 | class 对话记录(models.Model): 15 | id = models.IntegerField( 16 | primary_key = True, 17 | editable = False) 18 | 19 | role = models.TextField() 20 | content = models.TextField(null=True) 21 | 处理后content = models.TextField(null=True) 22 | 提交给大模型的playload = models.TextField(null=True) 23 | 不带入大模型对话中 = models.BooleanField(default=False) 24 | 已结束 = models.BooleanField(default=False) 25 | 26 | created_time = models.DateTimeField(auto_now_add=True) 27 | lastmodified_time = models.DateTimeField(auto_now=True) -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/home/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path 2 | 3 | from . import views,views_api,api 4 | 5 | app_name = "home" 6 | urlpatterns = [ 7 | path("", views.index, name="index"), 8 | path("salescheck", views.salescheck, name="salescheck"), 9 | path("addsalescheck", views.addsalescheck, name="addsalescheck"), 10 | path("newtalk", views.newtalk, name="newtalk"), 11 | 12 | path("api/new-talk", views_api.开始新的对话api, name="api-new-talk"), 13 | path("api/get-query-paras", views_api.获取结构化数据查询参数api, name="api-get-query-paras"), 14 | path("api/answer-without-data", views_api.从数据库查不到相关数据时的操作api, name="api-answer-without-data"), 15 | path("api/answer-with-data", views_api.根据查询结果回答用户输入api, name="api-answer-with-data"), 16 | path("api/get-conversation-list", views_api.获取对话记录api, name="api-get-conversation-list"), 17 | path("api/knowledge-embedding-batch", api.对知识批量进行向量编码, name="knowledge-embedding-batch"), 18 | 19 | path("api/import-data", api.导入数据, name="import-data"), 20 | ] 21 | -------------------------------------------------------------------------------- /实战案例3/第20节/如何运行本节课程序.md: -------------------------------------------------------------------------------- 1 | # 第一部分 - 先完成第19节课 2 | 3 | 这里假设你已经完成了第19节课,那么所需要的虚拟环境、依赖、测试数据、数据库表结构、独立的向量编码服务都已经搭建和运行好了。 4 | 5 | # 第二部分 安装本节课所需依赖 6 | 7 | 1. 安装用于将好评和差评数据导出excel的 `django-import-export` 包 8 | 9 | ```powershell 10 | pip install django-import-export 11 | ``` 12 | 13 | # 第三部分 建立管理员账户 14 | 15 | 1. 建立管理员账户,以登录后台导出好评和差评数据 16 | 17 | ```powershell 18 | python manage.py createsuperuser 19 | ``` 20 | 21 | # 第四部分 更新本节课所需数据库结构 22 | 23 | 1. 切换到根目录下的 `实战案例3\第20节\程序\main` 目录 24 | 2. 运行以下命令更新数据库 25 | 26 | ```powershell 27 | python .\manage.py makemigrations 28 | python .\manage.py migrate 29 | ``` 30 | 31 | # 第五部分 运行和测试 32 | 33 | 1. 把网站运行起来 34 | 35 | ```powershell 36 | python .\manage.py runserver 37 | ``` 38 | 39 | 2. 打开浏览器访问 `http://127.0.0.1:8000/` 确认网站可以正常访问 40 | 41 | 3. 输入问题:`AD林的对象放在哪里?` 42 | 4. 系统回答应该带有答案和链接 43 | 5. 点击好评或差评按钮 44 | 6. 现在可以在管理员界面点击“导出”按钮导出数据 45 | 46 | ![最终结果](https://github.com/weiminye/time-geekbang-org-rag/blob/main/%E5%AE%9E%E6%88%98%E6%A1%88%E4%BE%8B3/%E7%AC%AC20%E8%8A%82/result.png "CSDN图标") 47 | -------------------------------------------------------------------------------- /实战案例3/第18节/main/home/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | from pgvector.django import VectorField 3 | 4 | class 销售入账记录(models.Model): 5 | id = models.AutoField(primary_key=True) 6 | 7 | 客户 = models.CharField(max_length=255) 8 | 客户向量编码 = VectorField(dimensions=1024,null=True,blank=True) 9 | 客户向量编码模型 = models.TextField(default="bge-large-zh-v1.5") 10 | 入账日期 = models.DateTimeField() 11 | 入账金额 = models.TextField(null=True) 12 | 已到账款项 = models.IntegerField(null=True) 13 | 剩余到账款项 = models.IntegerField(null=True) 14 | 15 | class 对话记录(models.Model): 16 | id = models.AutoField(primary_key=True) 17 | 18 | role = models.TextField() 19 | content = models.TextField(null=True) 20 | 处理后content = models.TextField(null=True) 21 | 提交给大模型的playload = models.TextField(null=True) 22 | 不带入大模型对话中 = models.BooleanField(default=False) 23 | 已结束 = models.BooleanField(default=False) 24 | 25 | created_time = models.DateTimeField(auto_now_add=True) 26 | lastmodified_time = models.DateTimeField(auto_now=True) -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path 2 | 3 | from . import views,views_api,api 4 | 5 | app_name = "home" 6 | urlpatterns = [ 7 | path("", views.index, name="index"), 8 | path("salescheck", views.salescheck, name="salescheck"), 9 | path("addsalescheck", views.addsalescheck, name="addsalescheck"), 10 | path("newtalk", views.newtalk, name="newtalk"), 11 | path("review", views.review, name="review"), 12 | 13 | path("api/new-talk", views_api.开始新的对话api, name="api-new-talk"), 14 | path("api/get-query-paras", views_api.获取结构化数据查询参数api, name="api-get-query-paras"), 15 | path("api/answer-without-data", views_api.从数据库查不到相关数据时的操作api, name="api-answer-without-data"), 16 | path("api/answer-with-data", views_api.根据查询结果回答用户输入api, name="api-answer-with-data"), 17 | path("api/get-conversation-list", views_api.获取对话记录api, name="api-get-conversation-list"), 18 | path("api/knowledge-embedding-batch", api.对知识批量进行向量编码, name="knowledge-embedding-batch"), 19 | 20 | path("api/import-data", api.导入数据, name="import-data"), 21 | ] 22 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path 2 | 3 | from . import views,views_api,api 4 | 5 | app_name = "home" 6 | urlpatterns = [ 7 | path("", views.index, name="index"), 8 | path("salescheck", views.salescheck, name="salescheck"), 9 | path("addsalescheck", views.addsalescheck, name="addsalescheck"), 10 | path("newtalk", views.newtalk, name="newtalk"), 11 | path("review", views.review, name="review"), 12 | 13 | path("api/new-talk", views_api.开始新的对话api, name="api-new-talk"), 14 | path("api/get-query-paras", views_api.获取结构化数据查询参数api, name="api-get-query-paras"), 15 | path("api/answer-without-data", views_api.从数据库查不到相关数据时的操作api, name="api-answer-without-data"), 16 | path("api/answer-with-data", views_api.根据查询结果回答用户输入api, name="api-answer-with-data"), 17 | path("api/get-conversation-list", views_api.获取对话记录api, name="api-get-conversation-list"), 18 | path("api/knowledge-embedding-batch", api.对知识批量进行向量编码, name="knowledge-embedding-batch"), 19 | 20 | path("api/import-data", api.导入数据, name="import-data"), 21 | ] 22 | -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/home/migrations/0013_remove_知识主表_created_time_and_more.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-11-11 10:06 2 | 3 | from django.db import migrations 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0012_remove_知识主表_创建时间'), 10 | ] 11 | 12 | operations = [ 13 | migrations.RemoveField( 14 | model_name='知识主表', 15 | name='created_time', 16 | ), 17 | migrations.RemoveField( 18 | model_name='知识主表', 19 | name='lastmodified_time', 20 | ), 21 | migrations.RemoveField( 22 | model_name='知识主表', 23 | name='作者', 24 | ), 25 | migrations.RemoveField( 26 | model_name='知识主表', 27 | name='权限', 28 | ), 29 | migrations.RemoveField( 30 | model_name='知识详细表', 31 | name='created_time', 32 | ), 33 | migrations.RemoveField( 34 | model_name='知识详细表', 35 | name='lastmodified_time', 36 | ), 37 | ] 38 | -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/migrations/0013_remove_知识主表_created_time_and_more.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-11-11 10:06 2 | 3 | from django.db import migrations 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0012_remove_知识主表_创建时间'), 10 | ] 11 | 12 | operations = [ 13 | migrations.RemoveField( 14 | model_name='知识主表', 15 | name='created_time', 16 | ), 17 | migrations.RemoveField( 18 | model_name='知识主表', 19 | name='lastmodified_time', 20 | ), 21 | migrations.RemoveField( 22 | model_name='知识主表', 23 | name='作者', 24 | ), 25 | migrations.RemoveField( 26 | model_name='知识主表', 27 | name='权限', 28 | ), 29 | migrations.RemoveField( 30 | model_name='知识详细表', 31 | name='created_time', 32 | ), 33 | migrations.RemoveField( 34 | model_name='知识详细表', 35 | name='lastmodified_time', 36 | ), 37 | ] 38 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/migrations/0013_remove_知识主表_created_time_and_more.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-11-11 10:06 2 | 3 | from django.db import migrations 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0012_remove_知识主表_创建时间'), 10 | ] 11 | 12 | operations = [ 13 | migrations.RemoveField( 14 | model_name='知识主表', 15 | name='created_time', 16 | ), 17 | migrations.RemoveField( 18 | model_name='知识主表', 19 | name='lastmodified_time', 20 | ), 21 | migrations.RemoveField( 22 | model_name='知识主表', 23 | name='作者', 24 | ), 25 | migrations.RemoveField( 26 | model_name='知识主表', 27 | name='权限', 28 | ), 29 | migrations.RemoveField( 30 | model_name='知识详细表', 31 | name='created_time', 32 | ), 33 | migrations.RemoveField( 34 | model_name='知识详细表', 35 | name='lastmodified_time', 36 | ), 37 | ] 38 | -------------------------------------------------------------------------------- /实战案例1/改造后/home/views_api.py: -------------------------------------------------------------------------------- 1 | from django.http import JsonResponse 2 | from django.views.decorators.csrf import csrf_exempt 3 | from django.core import serializers 4 | 5 | from .rag import * 6 | def 开始新的对话api(request): 7 | 开始新的对话() 8 | return JsonResponse({"code":200,"message":"已经成功开始新的对话"}) 9 | 10 | @csrf_exempt 11 | def 获取结构化数据查询参数api(request): 12 | 用户输入 = request.POST['question'] 13 | 14 | 查询参数 = 获取结构化数据查询参数(用户输入) 15 | return JsonResponse({"querydata":查询参数}) 16 | 17 | def 从数据库查不到相关数据时的操作api(request): 18 | 从数据库查不到相关数据时的操作() 19 | return JsonResponse({"code":200,"message":"已经成功执行从数据库查不到相关数据时的操作"}) 20 | 21 | @csrf_exempt 22 | def 根据查询结果回答用户输入api(request): 23 | 用户输入 = request.POST['question'] 24 | 查询结果 = request.POST['query-result'] 25 | 26 | 根据查询结果回答用户输入(查询结果,用户输入) 27 | return JsonResponse({"code":200,"message":"已经成功根据查询结果回答用户输入"}) 28 | 29 | def 获取对话记录api(request): 30 | conversation_list = 获取当前对话记录() 31 | conversation_list_json = serializers.serialize("json", list(conversation_list)) 32 | return JsonResponse({"conversationlist":conversation_list_json}) -------------------------------------------------------------------------------- /实战案例3/第18节/main/home/views_api.py: -------------------------------------------------------------------------------- 1 | from django.http import JsonResponse 2 | from django.views.decorators.csrf import csrf_exempt 3 | from django.core import serializers 4 | 5 | from .rag import * 6 | def 开始新的对话api(request): 7 | 开始新的对话() 8 | return JsonResponse({"code":200,"message":"已经成功开始新的对话"}) 9 | 10 | @csrf_exempt 11 | def 获取结构化数据查询参数api(request): 12 | 用户输入 = request.POST['question'] 13 | 14 | 查询参数 = 获取结构化数据查询参数(用户输入) 15 | return JsonResponse({"querydata":查询参数}) 16 | 17 | def 从数据库查不到相关数据时的操作api(request): 18 | 从数据库查不到相关数据时的操作() 19 | return JsonResponse({"code":200,"message":"已经成功执行从数据库查不到相关数据时的操作"}) 20 | 21 | @csrf_exempt 22 | def 根据查询结果回答用户输入api(request): 23 | 用户输入 = request.POST['question'] 24 | 查询结果 = request.POST['query-result'] 25 | 26 | 根据查询结果回答用户输入(查询结果,用户输入) 27 | return JsonResponse({"code":200,"message":"已经成功根据查询结果回答用户输入"}) 28 | 29 | def 获取对话记录api(request): 30 | conversation_list = 获取当前对话记录() 31 | conversation_list_json = serializers.serialize("json", list(conversation_list)) 32 | return JsonResponse({"conversationlist":conversation_list_json}) -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/home/views_api.py: -------------------------------------------------------------------------------- 1 | from django.http import JsonResponse 2 | from django.views.decorators.csrf import csrf_exempt 3 | from django.core import serializers 4 | 5 | from .rag import * 6 | def 开始新的对话api(request): 7 | 开始新的对话() 8 | return JsonResponse({"code":200,"message":"已经成功开始新的对话"}) 9 | 10 | @csrf_exempt 11 | def 获取结构化数据查询参数api(request): 12 | 用户输入 = request.POST['question'] 13 | 14 | 查询参数 = 获取结构化数据查询参数(用户输入) 15 | return JsonResponse({"querydata":查询参数}) 16 | 17 | def 从数据库查不到相关数据时的操作api(request): 18 | 从数据库查不到相关数据时的操作() 19 | return JsonResponse({"code":200,"message":"已经成功执行从数据库查不到相关数据时的操作"}) 20 | 21 | @csrf_exempt 22 | def 根据查询结果回答用户输入api(request): 23 | 用户输入 = request.POST['question'] 24 | 查询结果 = request.POST['query-result'] 25 | 26 | 根据查询结果回答用户输入(查询结果,用户输入) 27 | return JsonResponse({"code":200,"message":"已经成功根据查询结果回答用户输入"}) 28 | 29 | def 获取对话记录api(request): 30 | conversation_list = 获取当前对话记录() 31 | conversation_list_json = serializers.serialize("json", list(conversation_list)) 32 | return JsonResponse({"conversationlist":conversation_list_json}) -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/views_api.py: -------------------------------------------------------------------------------- 1 | from django.http import JsonResponse 2 | from django.views.decorators.csrf import csrf_exempt 3 | from django.core import serializers 4 | 5 | from .rag import * 6 | def 开始新的对话api(request): 7 | 开始新的对话() 8 | return JsonResponse({"code":200,"message":"已经成功开始新的对话"}) 9 | 10 | @csrf_exempt 11 | def 获取结构化数据查询参数api(request): 12 | 用户输入 = request.POST['question'] 13 | 14 | 查询参数 = 获取结构化数据查询参数(用户输入) 15 | return JsonResponse({"querydata":查询参数}) 16 | 17 | def 从数据库查不到相关数据时的操作api(request): 18 | 从数据库查不到相关数据时的操作() 19 | return JsonResponse({"code":200,"message":"已经成功执行从数据库查不到相关数据时的操作"}) 20 | 21 | @csrf_exempt 22 | def 根据查询结果回答用户输入api(request): 23 | 用户输入 = request.POST['question'] 24 | 查询结果 = request.POST['query-result'] 25 | 26 | 根据查询结果回答用户输入(查询结果,用户输入) 27 | return JsonResponse({"code":200,"message":"已经成功根据查询结果回答用户输入"}) 28 | 29 | def 获取对话记录api(request): 30 | conversation_list = 获取当前对话记录() 31 | conversation_list_json = serializers.serialize("json", list(conversation_list)) 32 | return JsonResponse({"conversationlist":conversation_list_json}) -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/views_api.py: -------------------------------------------------------------------------------- 1 | from django.http import JsonResponse 2 | from django.views.decorators.csrf import csrf_exempt 3 | from django.core import serializers 4 | 5 | from .rag import * 6 | def 开始新的对话api(request): 7 | 开始新的对话() 8 | return JsonResponse({"code":200,"message":"已经成功开始新的对话"}) 9 | 10 | @csrf_exempt 11 | def 获取结构化数据查询参数api(request): 12 | 用户输入 = request.POST['question'] 13 | 14 | 查询参数 = 获取结构化数据查询参数(用户输入) 15 | return JsonResponse({"querydata":查询参数}) 16 | 17 | def 从数据库查不到相关数据时的操作api(request): 18 | 从数据库查不到相关数据时的操作() 19 | return JsonResponse({"code":200,"message":"已经成功执行从数据库查不到相关数据时的操作"}) 20 | 21 | @csrf_exempt 22 | def 根据查询结果回答用户输入api(request): 23 | 用户输入 = request.POST['question'] 24 | 查询结果 = request.POST['query-result'] 25 | 26 | 根据查询结果回答用户输入(查询结果,用户输入) 27 | return JsonResponse({"code":200,"message":"已经成功根据查询结果回答用户输入"}) 28 | 29 | def 获取对话记录api(request): 30 | conversation_list = 获取当前对话记录() 31 | conversation_list_json = serializers.serialize("json", list(conversation_list)) 32 | return JsonResponse({"conversationlist":conversation_list_json}) -------------------------------------------------------------------------------- /实战案例1/改造后/templates/home/index.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block main_content%} 4 |
5 |

首页

6 |
7 | 8 |
9 | {% for current_obj in object_list %} 10 | {% if current_obj.不带入大模型对话中 is False%} 11 |
12 | {% if current_obj.role == "user" %} 13 | 你:{{current_obj.content}} 14 | {% else %} 15 | AI:{{current_obj.处理后content}} 16 | {% endif %} 17 |
18 | {% endif %} 19 | {% endfor %} 20 |
21 | {% csrf_token %} 22 |
23 | 24 |
25 | 26 |
27 |   28 |
29 |
30 | 开始新的对话 31 |
32 | {% endblock %} -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/templates/home/index.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block main_content%} 4 |
5 |

首页

6 |
7 | 8 |
9 | {% for current_obj in object_list %} 10 | {% if current_obj.不带入大模型对话中 is False%} 11 |
12 | {% if current_obj.role == "user" %} 13 | 你:{{current_obj.content}} 14 | {% else %} 15 | AI:{{current_obj.处理后content}} 16 | {% endif %} 17 |
18 | {% endif %} 19 | {% endfor %} 20 |
21 | {% csrf_token %} 22 |
23 | 24 |
25 | 26 |
27 |   28 |
29 |
30 | 开始新的对话 31 |
32 | {% endblock %} -------------------------------------------------------------------------------- /实战案例3/第18节/main/templates/home/index.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block main_content%} 4 |
5 |

首页

6 |
7 | 8 |
9 | {% for current_obj in object_list %} 10 | {% if current_obj.不带入大模型对话中 is False%} 11 |
12 | {% if current_obj.role == "user" %} 13 | 你:{{current_obj.content}} 14 | {% else %} 15 | AI:{{current_obj.处理后content}} 16 | {% endif %} 17 |
18 | {% endif %} 19 | {% endfor %} 20 |
21 | {% csrf_token %} 22 |
23 | 24 |
25 | 26 |
27 |   28 |
29 |
30 | 开始新的对话 31 |
32 | {% endblock %} -------------------------------------------------------------------------------- /实战案例3/第18节/main/home/search.py: -------------------------------------------------------------------------------- 1 | from .models import 销售入账记录 2 | from .api import 调用向量编码服务 3 | 4 | def 查询(查询参数): 5 | if '模块' in 查询参数: 6 | if 查询参数['模块'] == 1: #'销售对账' 7 | if '客户名称' in 查询参数: 8 | 客户 = 查询参数['客户名称'].strip() 9 | 精确搜索结果 = 销售入账记录.objects.filter(客户=客户).values('客户', '入账日期', '入账金额', '已到账款项', '剩余到账款项') 10 | if 精确搜索结果 is not None and len(精确搜索结果) > 0: 11 | return 精确搜索结果 12 | else: 13 | 查询字符串向量编码 = 调用向量编码服务(客户) 14 | 模糊搜索结果RawQuerySet = 销售入账记录.objects.raw('SELECT *, 1 - (客户向量编码 <=> %s) AS 余弦相似度,客户 FROM public."home_销售入账记录" order by 余弦相似度 desc;',[str(查询字符串向量编码),]) 15 | 16 | 模糊搜索结果 = [] 17 | for item in 模糊搜索结果RawQuerySet: 18 | 模糊搜索结果.append({ 19 | '客户': item.客户, 20 | '入账日期': str(item.入账日期), 21 | '入账金额': item.入账金额, 22 | '已到账款项': item.已到账款项, 23 | '剩余到账款项': item.剩余到账款项 24 | }) 25 | return 模糊搜索结果 -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/home/migrations/0007_cnet新闻.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-09-03 00:44 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('home', '0006_对话记录_提交给大模型的playload_alter_对话记录_content_and_more'), 10 | ] 11 | 12 | operations = [ 13 | migrations.CreateModel( 14 | name='CNET新闻', 15 | fields=[ 16 | ('id', models.IntegerField(editable=False, primary_key=True, serialize=False)), 17 | ('标题', models.TextField()), 18 | ('标题中文翻译', models.TextField(null=True)), 19 | ('新闻发布日期', models.DateTimeField(auto_now_add=True)), 20 | ('url', models.TextField(null=True)), 21 | ('作者', models.TextField(null=True)), 22 | ('权限', models.TextField(null=True)), 23 | ('新闻内容', models.TextField(default=False)), 24 | ('摘要', models.TextField(default=False)), 25 | ('新闻内容中文翻译', models.TextField(default=False)), 26 | ('created_time', models.DateTimeField(auto_now_add=True)), 27 | ('lastmodified_time', models.DateTimeField(auto_now=True)), 28 | ], 29 | ), 30 | ] 31 | -------------------------------------------------------------------------------- /实战案例1/改造前/home/views.py: -------------------------------------------------------------------------------- 1 | from django.http import HttpResponse 2 | from django.shortcuts import redirect, render 3 | from django.urls import reverse 4 | 5 | from .search import 查询 6 | 7 | from .models import 销售入账记录 8 | 9 | def index(request): 10 | # return HttpResponse("home index") 11 | return render(request, "home/index.html") 12 | 13 | def salescheck(request): 14 | # return HttpResponse("home index") 15 | if request.method == 'POST': 16 | 客户 = request.POST['name'] 17 | if 客户 is not None or 客户.strip() != '': 18 | object_list = 查询({'模块':'销售对账','客户名称':客户}) 19 | else: 20 | object_list = 销售入账记录.objects.all() 21 | else: 22 | object_list = 销售入账记录.objects.all() 23 | 24 | return render(request, "home/salescheck.html",context={"object_list":object_list}) 25 | 26 | def addsalescheck(request): 27 | if request.method == 'POST': 28 | record = 销售入账记录() 29 | record.客户 = request.POST['name'] 30 | record.入账日期 = request.POST['created_at'] 31 | record.入账金额 = request.POST['amount'] 32 | record.已到账款项 = request.POST['total'] 33 | record.剩余到账款项 = request.POST['leave'] 34 | 35 | record.save() 36 | return redirect(reverse('home:salescheck')) 37 | else: 38 | return render(request, "home/addsalescheck.html") 39 | -------------------------------------------------------------------------------- /实战案例1/改造前/home/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | from django.db import migrations, models 2 | from django.db import transaction 3 | 4 | 销售入账记录_data = [ 5 | {'id': 1, '客户': '广州神机妙算有限公司', '入账日期': '2024-07-06', '入账金额': 9527, '已到账款项': 57980, '剩余到账款项': 2908}, 6 | {'id': 2, '客户': '北京测试数据有限公司', '入账日期': '2024-06-01', '入账金额': 996, '已到账款项': 7653, '剩余到账款项': 2521}, 7 | {'id': 3, '客户': '宇宙洪荒测试公司', '入账日期': '2024-04-05', '入账金额': 251, '已到账款项': 8960, '剩余到账款项': 22463}, 8 | ] 9 | 10 | 11 | def initial_data(apps, schema_editor): 12 | model = apps.get_model("home", "销售入账记录") 13 | db_alias = schema_editor.connection.alias 14 | 15 | with transaction.atomic(): 16 | for data in 销售入账记录_data: 17 | model .objects.using(db_alias).create(**data) 18 | 19 | class Migration(migrations.Migration): 20 | 21 | initial = True 22 | 23 | dependencies = [ 24 | ] 25 | 26 | operations = [ 27 | migrations.CreateModel( 28 | name='销售入账记录', 29 | fields=[ 30 | ('id', models.IntegerField(editable=False, primary_key=True, serialize=False)), 31 | ('客户', models.CharField(max_length=255)), 32 | ('入账日期', models.DateTimeField()), 33 | ('入账金额', models.TextField(null=True)), 34 | ('已到账款项', models.IntegerField(null=True)), 35 | ('剩余到账款项', models.IntegerField(null=True)), 36 | ], 37 | ), 38 | migrations.RunPython(initial_data), #添加这一行 39 | ] 40 | -------------------------------------------------------------------------------- /实战案例1/改造后/home/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | from django.db import migrations, models 2 | from django.db import transaction 3 | 4 | 销售入账记录_data = [ 5 | {'id': 1, '客户': '广州神机妙算有限公司', '入账日期': '2024-07-06', '入账金额': 9527, '已到账款项': 57980, '剩余到账款项': 2908}, 6 | {'id': 2, '客户': '北京测试数据有限公司', '入账日期': '2024-06-01', '入账金额': 996, '已到账款项': 7653, '剩余到账款项': 2521}, 7 | {'id': 3, '客户': '宇宙洪荒测试公司', '入账日期': '2024-04-05', '入账金额': 251, '已到账款项': 8960, '剩余到账款项': 22463}, 8 | ] 9 | 10 | 11 | def initial_data(apps, schema_editor): 12 | model = apps.get_model("home", "销售入账记录") 13 | db_alias = schema_editor.connection.alias 14 | 15 | with transaction.atomic(): 16 | for data in 销售入账记录_data: 17 | model .objects.using(db_alias).create(**data) 18 | 19 | class Migration(migrations.Migration): 20 | 21 | initial = True 22 | 23 | dependencies = [ 24 | ] 25 | 26 | operations = [ 27 | migrations.CreateModel( 28 | name='销售入账记录', 29 | fields=[ 30 | ('id', models.IntegerField(editable=False, primary_key=True, serialize=False)), 31 | ('客户', models.CharField(max_length=255)), 32 | ('入账日期', models.DateTimeField()), 33 | ('入账金额', models.TextField(null=True)), 34 | ('已到账款项', models.IntegerField(null=True)), 35 | ('剩余到账款项', models.IntegerField(null=True)), 36 | ], 37 | ), 38 | migrations.RunPython(initial_data), #添加这一行 39 | ] 40 | -------------------------------------------------------------------------------- /实战案例3/第18节/main/home/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | from django.db import migrations, models 2 | from django.db import transaction 3 | 4 | 销售入账记录_data = [ 5 | {'id': 1, '客户': '广州神机妙算有限公司', '入账日期': '2024-07-06', '入账金额': 9527, '已到账款项': 57980, '剩余到账款项': 2908}, 6 | {'id': 2, '客户': '北京测试数据有限公司', '入账日期': '2024-06-01', '入账金额': 996, '已到账款项': 7653, '剩余到账款项': 2521}, 7 | {'id': 3, '客户': '宇宙洪荒测试公司', '入账日期': '2024-04-05', '入账金额': 251, '已到账款项': 8960, '剩余到账款项': 22463}, 8 | ] 9 | 10 | 11 | def initial_data(apps, schema_editor): 12 | model = apps.get_model("home", "销售入账记录") 13 | db_alias = schema_editor.connection.alias 14 | 15 | with transaction.atomic(): 16 | for data in 销售入账记录_data: 17 | model .objects.using(db_alias).create(**data) 18 | 19 | class Migration(migrations.Migration): 20 | 21 | initial = True 22 | 23 | dependencies = [ 24 | ] 25 | 26 | operations = [ 27 | migrations.CreateModel( 28 | name='销售入账记录', 29 | fields=[ 30 | ('id', models.IntegerField(editable=False, primary_key=True, serialize=False)), 31 | ('客户', models.CharField(max_length=255)), 32 | ('入账日期', models.DateTimeField()), 33 | ('入账金额', models.TextField(null=True)), 34 | ('已到账款项', models.IntegerField(null=True)), 35 | ('剩余到账款项', models.IntegerField(null=True)), 36 | ], 37 | ), 38 | migrations.RunPython(initial_data), #添加这一行 39 | ] 40 | -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/home/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | from django.db import migrations, models 2 | from django.db import transaction 3 | 4 | 销售入账记录_data = [ 5 | {'id': 1, '客户': '广州神机妙算有限公司', '入账日期': '2024-07-06', '入账金额': 9527, '已到账款项': 57980, '剩余到账款项': 2908}, 6 | {'id': 2, '客户': '北京测试数据有限公司', '入账日期': '2024-06-01', '入账金额': 996, '已到账款项': 7653, '剩余到账款项': 2521}, 7 | {'id': 3, '客户': '宇宙洪荒测试公司', '入账日期': '2024-04-05', '入账金额': 251, '已到账款项': 8960, '剩余到账款项': 22463}, 8 | ] 9 | 10 | 11 | def initial_data(apps, schema_editor): 12 | model = apps.get_model("home", "销售入账记录") 13 | db_alias = schema_editor.connection.alias 14 | 15 | with transaction.atomic(): 16 | for data in 销售入账记录_data: 17 | model .objects.using(db_alias).create(**data) 18 | 19 | class Migration(migrations.Migration): 20 | 21 | initial = True 22 | 23 | dependencies = [ 24 | ] 25 | 26 | operations = [ 27 | migrations.CreateModel( 28 | name='销售入账记录', 29 | fields=[ 30 | ('id', models.IntegerField(editable=False, primary_key=True, serialize=False)), 31 | ('客户', models.CharField(max_length=255)), 32 | ('入账日期', models.DateTimeField()), 33 | ('入账金额', models.TextField(null=True)), 34 | ('已到账款项', models.IntegerField(null=True)), 35 | ('剩余到账款项', models.IntegerField(null=True)), 36 | ], 37 | ), 38 | migrations.RunPython(initial_data), #添加这一行 39 | ] 40 | -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/home/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | from django.db import migrations, models 2 | from django.db import transaction 3 | 4 | 销售入账记录_data = [ 5 | {'id': 1, '客户': '广州神机妙算有限公司', '入账日期': '2024-07-06', '入账金额': 9527, '已到账款项': 57980, '剩余到账款项': 2908}, 6 | {'id': 2, '客户': '北京测试数据有限公司', '入账日期': '2024-06-01', '入账金额': 996, '已到账款项': 7653, '剩余到账款项': 2521}, 7 | {'id': 3, '客户': '宇宙洪荒测试公司', '入账日期': '2024-04-05', '入账金额': 251, '已到账款项': 8960, '剩余到账款项': 22463}, 8 | ] 9 | 10 | 11 | def initial_data(apps, schema_editor): 12 | model = apps.get_model("home", "销售入账记录") 13 | db_alias = schema_editor.connection.alias 14 | 15 | with transaction.atomic(): 16 | for data in 销售入账记录_data: 17 | model .objects.using(db_alias).create(**data) 18 | 19 | class Migration(migrations.Migration): 20 | 21 | initial = True 22 | 23 | dependencies = [ 24 | ] 25 | 26 | operations = [ 27 | migrations.CreateModel( 28 | name='销售入账记录', 29 | fields=[ 30 | ('id', models.IntegerField(editable=False, primary_key=True, serialize=False)), 31 | ('客户', models.CharField(max_length=255)), 32 | ('入账日期', models.DateTimeField()), 33 | ('入账金额', models.TextField(null=True)), 34 | ('已到账款项', models.IntegerField(null=True)), 35 | ('剩余到账款项', models.IntegerField(null=True)), 36 | ], 37 | ), 38 | migrations.RunPython(initial_data), #添加这一行 39 | ] 40 | -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/templates/home/index.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block main_content%} 4 |
5 |

首页

6 |
7 | 8 |
9 | {% if question is not None %} 10 |
11 | 你:{{question}} 12 |
13 | {% endif %} 14 | {% for current_obj in object_list %} 15 | {% if current_obj.不带入大模型对话中 is False%} 16 |
17 | {% if current_obj.role == "user" %} 18 | 你:{{current_obj.content}} 19 |
20 | {% else %} 21 | AI:{{current_obj.处理后content}} 22 | {% if link_text is not None %} 23 |
24 | 参考链接:{{link_text}}   25 |
26 | {% endif %} 27 |
28 | {% endif %} 29 |
30 | {% endif %} 31 | {% endfor %} 32 | 33 |
34 | {% csrf_token %} 35 |
36 | 37 |
38 | 39 |
40 |   41 |
42 |
43 | 开始新的对话 44 |
45 | {% endblock %} -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | from django.db import migrations, models 2 | from django.db import transaction 3 | 4 | 销售入账记录_data = [ 5 | {'id': 1, '客户': '广州神机妙算有限公司', '入账日期': '2024-07-06', '入账金额': 9527, '已到账款项': 57980, '剩余到账款项': 2908}, 6 | {'id': 2, '客户': '北京测试数据有限公司', '入账日期': '2024-06-01', '入账金额': 996, '已到账款项': 7653, '剩余到账款项': 2521}, 7 | {'id': 3, '客户': '宇宙洪荒测试公司', '入账日期': '2024-04-05', '入账金额': 251, '已到账款项': 8960, '剩余到账款项': 22463}, 8 | ] 9 | 10 | 11 | def initial_data(apps, schema_editor): 12 | model = apps.get_model("home", "销售入账记录") 13 | db_alias = schema_editor.connection.alias 14 | 15 | with transaction.atomic(): 16 | for data in 销售入账记录_data: 17 | model .objects.using(db_alias).create(**data) 18 | 19 | class Migration(migrations.Migration): 20 | 21 | initial = True 22 | 23 | dependencies = [ 24 | ] 25 | 26 | operations = [ 27 | migrations.CreateModel( 28 | name='销售入账记录', 29 | fields=[ 30 | ('id', models.IntegerField(editable=False, primary_key=True, serialize=False)), 31 | ('客户', models.CharField(max_length=255)), 32 | ('入账日期', models.DateTimeField()), 33 | ('入账金额', models.TextField(null=True)), 34 | ('已到账款项', models.IntegerField(null=True)), 35 | ('剩余到账款项', models.IntegerField(null=True)), 36 | ], 37 | ), 38 | migrations.RunPython(initial_data), #添加这一行 39 | ] 40 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | from django.db import migrations, models 2 | from django.db import transaction 3 | 4 | 销售入账记录_data = [ 5 | {'id': 1, '客户': '广州神机妙算有限公司', '入账日期': '2024-07-06', '入账金额': 9527, '已到账款项': 57980, '剩余到账款项': 2908}, 6 | {'id': 2, '客户': '北京测试数据有限公司', '入账日期': '2024-06-01', '入账金额': 996, '已到账款项': 7653, '剩余到账款项': 2521}, 7 | {'id': 3, '客户': '宇宙洪荒测试公司', '入账日期': '2024-04-05', '入账金额': 251, '已到账款项': 8960, '剩余到账款项': 22463}, 8 | ] 9 | 10 | 11 | def initial_data(apps, schema_editor): 12 | model = apps.get_model("home", "销售入账记录") 13 | db_alias = schema_editor.connection.alias 14 | 15 | with transaction.atomic(): 16 | for data in 销售入账记录_data: 17 | model .objects.using(db_alias).create(**data) 18 | 19 | class Migration(migrations.Migration): 20 | 21 | initial = True 22 | 23 | dependencies = [ 24 | ] 25 | 26 | operations = [ 27 | migrations.CreateModel( 28 | name='销售入账记录', 29 | fields=[ 30 | ('id', models.IntegerField(editable=False, primary_key=True, serialize=False)), 31 | ('客户', models.CharField(max_length=255)), 32 | ('入账日期', models.DateTimeField()), 33 | ('入账金额', models.TextField(null=True)), 34 | ('已到账款项', models.IntegerField(null=True)), 35 | ('剩余到账款项', models.IntegerField(null=True)), 36 | ], 37 | ), 38 | migrations.RunPython(initial_data), #添加这一行 39 | ] 40 | -------------------------------------------------------------------------------- /实战案例3/第19节/如何运行本节课程序.md: -------------------------------------------------------------------------------- 1 | # 第一部分 - 更新数据库表和启动网站 2 | 1. 打开 `anaconda` 运行以下命令激活虚拟环境 `rag2` 3 | 4 | ```powershell 5 | conda activate rag2 6 | ``` 7 | 8 | 2. 切换到根目录下的 `实战案例3\第19节\程序\main` 目录 9 | 3. 运行以下命令更新数据库 10 | 11 | ```powershell 12 | python .\manage.py makemigrations 13 | python .\manage.py migrate 14 | ``` 15 | 16 | 4. 打开 `pgadmin` 确认数据库表都创建无误。 17 | 18 | 5. 运行以下命令把website跑起来: 19 | 20 | ```powershell 21 | python .\manage.py runserver 22 | ``` 23 | 24 | 6. 打开浏览器访问 `http://127.0.0.1:8000/` 确认网站可以正常访问 25 | 26 | # 第二部分 启动嵌入模型服务 27 | 28 | 1. 新开一个 `anaconda` 并激活虚拟环境 `rag2` 29 | 30 | ```powershell 31 | conda activate rag2 32 | ``` 33 | 34 | 2. 切换到根目录下的 `实战案例3\嵌入模型服务` 目录 35 | 36 | 3. 运行以下命令把向量编码服务运行起来 37 | 38 | ```powershell 39 | python .\main.py 40 | ``` 41 | 42 | 4. 打开浏览器访问 `http://127.0.0.1:8902/` 确认网站可以正常访问 43 | 44 | # 第三部分 导入数据 45 | 46 | 1. 新开一个 `anaconda` 并激活虚拟环境 `rag2` 47 | 48 | ```powershell 49 | conda activate rag2 50 | ``` 51 | 52 | 2. 切换到根目录下的 `实战案例3\第19节\要导入的数据` 目录 53 | 54 | 3. 安装依赖: 55 | 56 | ```powershell 57 | pip install pandas 58 | pip install openpyxl 59 | ``` 60 | 61 | 4. 运行 `要导入的数据` 目录下的 `import_data_from_excel.py` 文件,把 `data.xlsx` 的数据导入到数据库 62 | 63 | ```powershell 64 | python .\import_data_from_excel.py 65 | ``` 66 | 67 | 5. 打开 `pgadmin` 确认数据都导入无误。 68 | 69 | # 第四部分 测试 70 | 71 | 1. 回到在第一部分打开的 `http://127.0.0.1:8000/` 浏览器 72 | 2. 输入问题:`AD林的对象放在哪里?` 73 | 3. 系统回答应该带有答案和链接 74 | 75 | ![最终结果](https://github.com/weiminye/time-geekbang-org-rag/blob/main/%E5%AE%9E%E6%88%98%E6%A1%88%E4%BE%8B3/%E7%AC%AC19%E8%8A%82/result.png "CSDN图标") 76 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/search.py: -------------------------------------------------------------------------------- 1 | from .models import 销售入账记录 2 | from .api import 调用向量编码服务 3 | 4 | def 查询(查询参数): 5 | if '模块' in 查询参数: 6 | if 查询参数['模块'] == 1: #'销售对账' 7 | if '客户名称' in 查询参数: 8 | 客户 = 查询参数['客户名称'].strip() 9 | 精确搜索结果QuerySet = 销售入账记录.objects.filter(客户=客户).values('客户', '入账日期', '入账金额', '已到账款项', '剩余到账款项') 10 | if 精确搜索结果QuerySet is not None and len(精确搜索结果QuerySet) > 0: 11 | 精确搜索结果 = [] 12 | for item in 精确搜索结果QuerySet: 13 | 精确搜索结果.append({ 14 | '客户': item['客户'], 15 | '入账日期': str(item['入账日期']), 16 | '入账金额': item['入账金额'], 17 | '已到账款项': item['已到账款项'], 18 | '剩余到账款项': item['剩余到账款项'] 19 | }) 20 | return 精确搜索结果 21 | else: 22 | 查询字符串向量编码 = 调用向量编码服务(客户) 23 | 模糊搜索结果RawQuerySet = 销售入账记录.objects.raw('SELECT *, 1 - (客户向量编码 <=> %s) AS 余弦相似度,客户 FROM public."home_销售入账记录" order by 余弦相似度 desc;',[str(查询字符串向量编码),]) 24 | 25 | 模糊搜索结果 = [] 26 | for item in 模糊搜索结果RawQuerySet: 27 | 模糊搜索结果.append({ 28 | '客户': item.客户, 29 | '入账日期': str(item.入账日期), 30 | '入账金额': item.入账金额, 31 | '已到账款项': item.已到账款项, 32 | '剩余到账款项': item.剩余到账款项 33 | }) 34 | return 模糊搜索结果 -------------------------------------------------------------------------------- /实战案例2/新闻.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | class 新闻: 4 | def __init__(self): 5 | self.元数据 = 元数据() 6 | self.新闻内容 = None 7 | self.新闻内容_中文翻译 = None 8 | self.摘要 = None 9 | 10 | def set_元数据(self, 元数据): 11 | self.元数据 = 元数据 12 | 13 | def set_新闻内容(self, 新闻内容): 14 | self.新闻内容 = 新闻内容 15 | 16 | def set_新闻内容_中文翻译(self, 新闻内容_中文翻译): 17 | self.新闻内容_中文翻译 = 新闻内容_中文翻译 18 | 19 | def set_标题_中文翻译(self, 标题_中文翻译): 20 | self.元数据.标题_中文翻译 = 标题_中文翻译 21 | 22 | def set_摘要(self, 摘要): 23 | self.摘要 = 摘要 24 | 25 | class 新闻Encoder(json.JSONEncoder): 26 | def default(self, obj): 27 | if isinstance(obj, 新闻): 28 | return {"元数据": json.loads(json.dumps(obj.元数据, cls=元数据Encoder)), "新闻内容": obj.新闻内容,"新闻内容_中文翻译":obj.新闻内容_中文翻译,"摘要": obj.摘要} 29 | return super().default(obj) 30 | 31 | class 元数据: 32 | def __init__(self): 33 | self.标题 = None 34 | self.标题_中文翻译 = None 35 | self.作者 = None 36 | self.创建日期 = None 37 | self.url = None 38 | 39 | def set_标题(self, 标题): 40 | self.标题 = 标题 41 | 42 | def set_标题_中文翻译(self, 标题_中文翻译): 43 | self.标题_中文翻译 = 标题_中文翻译 44 | 45 | def set_作者(self, 作者): 46 | self.作者 = 作者 47 | 48 | def set_创建日期(self, 创建日期): 49 | self.创建日期 = 创建日期 50 | 51 | def set_url(self, url): 52 | self.url = url 53 | 54 | class 元数据Encoder(json.JSONEncoder): 55 | def default(self, obj): 56 | if isinstance(obj, 元数据): 57 | return {"标题": obj.标题,"标题_中文翻译": obj.标题_中文翻译, "作者": obj.作者, "创建日期": obj.创建日期, "url": obj.url} 58 | return super().default(obj) -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/home/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | class 销售入账记录(models.Model): 4 | id = models.IntegerField( 5 | primary_key = True, 6 | editable = False) 7 | 8 | 客户 = models.CharField(max_length=255) 9 | 入账日期 = models.DateTimeField() 10 | 入账金额 = models.TextField(null=True) 11 | 已到账款项 = models.IntegerField(null=True) 12 | 剩余到账款项 = models.IntegerField(null=True) 13 | 14 | class 对话记录(models.Model): 15 | id = models.IntegerField( 16 | primary_key = True, 17 | editable = False) 18 | 19 | role = models.TextField() 20 | content = models.TextField(null=True) 21 | 处理后content = models.TextField(null=True) 22 | 提交给大模型的playload = models.TextField(null=True) 23 | 不带入大模型对话中 = models.BooleanField(default=False) 24 | 已结束 = models.BooleanField(default=False) 25 | 26 | created_time = models.DateTimeField(auto_now_add=True) 27 | lastmodified_time = models.DateTimeField(auto_now=True) 28 | 29 | class CNET新闻(models.Model): 30 | id = models.IntegerField( 31 | primary_key = True, 32 | editable = False) 33 | 34 | 标题 = models.TextField() 35 | 标题中文翻译 = models.TextField(null=True) 36 | 新闻发布日期 = models.DateTimeField(null=True) 37 | url = models.TextField(null=True) 38 | 作者 = models.TextField(null=True) 39 | 权限 = models.TextField(null=True) 40 | 新闻内容 = models.TextField(default=False) 41 | 摘要 = models.TextField(default=False) 42 | 新闻内容中文翻译 = models.TextField(default=False) 43 | 44 | created_time = models.DateTimeField(auto_now_add=True) 45 | lastmodified_time = models.DateTimeField(auto_now=True) -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/home/search.py: -------------------------------------------------------------------------------- 1 | from .models import 知识主表, 知识详细表, 销售入账记录 2 | from .api import 调用向量编码服务 3 | 4 | def 查询(查询参数): 5 | if '模块' in 查询参数: 6 | if 查询参数['模块'] == 1: #'销售对账' 7 | if '客户名称' in 查询参数: 8 | 客户 = 查询参数['客户名称'].strip() 9 | 精确搜索结果 = 销售入账记录.objects.filter(客户=客户).values('客户', '入账日期', '入账金额', '已到账款项', '剩余到账款项') 10 | if 精确搜索结果 is not None and len(精确搜索结果) > 0: 11 | return 精确搜索结果 12 | else: 13 | 查询字符串向量编码 = 调用向量编码服务(客户) 14 | 模糊搜索结果RawQuerySet = 销售入账记录.objects.raw('SELECT *, 1 - (客户向量编码 <=> %s) AS 余弦相似度,客户 FROM public."home_销售入账记录" order by 余弦相似度 desc;',[str(查询字符串向量编码),]) 15 | 16 | 模糊搜索结果 = [] 17 | for item in 模糊搜索结果RawQuerySet: 18 | 模糊搜索结果.append({ 19 | '客户': item.客户, 20 | '入账日期': str(item.入账日期), 21 | '入账金额': item.入账金额, 22 | '已到账款项': item.已到账款项, 23 | '剩余到账款项': item.剩余到账款项 24 | }) 25 | return 模糊搜索结果 26 | else: 27 | 查询字符串 = 查询参数.strip() 28 | 查询字符串向量编码 = 调用向量编码服务(查询字符串) 29 | 模糊搜索结果RawQuerySet = 知识详细表.objects.raw('SELECT *, 1 - (向量编码 <=> %s) AS 余弦相似度,文本内容 FROM public."home_知识详细表" order by 余弦相似度 desc;',[str(查询字符串向量编码),]) 30 | 31 | 选取的结果 = 模糊搜索结果RawQuerySet[0] 32 | 模糊搜索结果 = { 33 | '知识内容': 选取的结果.文本内容, 34 | 'url': 选取的结果.知识主表.url, 35 | '标题': 选取的结果.知识主表.标题 36 | } 37 | return 模糊搜索结果 -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/search.py: -------------------------------------------------------------------------------- 1 | from .models import 知识主表, 知识详细表, 销售入账记录 2 | from .api import 调用向量编码服务 3 | 4 | def 查询(查询参数): 5 | if '模块' in 查询参数: 6 | if 查询参数['模块'] == 1: #'销售对账' 7 | if '客户名称' in 查询参数: 8 | 客户 = 查询参数['客户名称'].strip() 9 | 精确搜索结果 = 销售入账记录.objects.filter(客户=客户).values('客户', '入账日期', '入账金额', '已到账款项', '剩余到账款项') 10 | if 精确搜索结果 is not None and len(精确搜索结果) > 0: 11 | return 精确搜索结果 12 | else: 13 | 查询字符串向量编码 = 调用向量编码服务(客户) 14 | 模糊搜索结果RawQuerySet = 销售入账记录.objects.raw('SELECT *, 1 - (客户向量编码 <=> %s) AS 余弦相似度,客户 FROM public."home_销售入账记录" order by 余弦相似度 desc;',[str(查询字符串向量编码),]) 15 | 16 | 模糊搜索结果 = [] 17 | for item in 模糊搜索结果RawQuerySet: 18 | 模糊搜索结果.append({ 19 | '客户': item.客户, 20 | '入账日期': str(item.入账日期), 21 | '入账金额': item.入账金额, 22 | '已到账款项': item.已到账款项, 23 | '剩余到账款项': item.剩余到账款项 24 | }) 25 | return 模糊搜索结果 26 | else: 27 | 查询字符串 = 查询参数.strip() 28 | 查询字符串向量编码 = 调用向量编码服务(查询字符串) 29 | 模糊搜索结果RawQuerySet = 知识详细表.objects.raw('SELECT *, 1 - (向量编码 <=> %s) AS 余弦相似度,文本内容 FROM public."home_知识详细表" order by 余弦相似度 desc;',[str(查询字符串向量编码),]) 30 | 31 | 选取的结果 = 模糊搜索结果RawQuerySet[0] 32 | 模糊搜索结果 = { 33 | '知识内容': 选取的结果.文本内容, 34 | 'url': 选取的结果.知识主表.url, 35 | '标题': 选取的结果.知识主表.标题 36 | } 37 | return 模糊搜索结果 -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/home/api.py: -------------------------------------------------------------------------------- 1 | import json 2 | from django.http import JsonResponse 3 | from django.views.decorators.csrf import csrf_exempt 4 | import requests 5 | 6 | from .models import 知识主表, 知识详细表, 销售入账记录 7 | def 调用向量编码服务(输入字符串): 8 | body_json = { 9 | "input":输入字符串 10 | } 11 | response = requests.post(f"http://127.0.0.1:8902/api/embedding/encode", json=body_json) 12 | if response.status_code == 200: 13 | return json.loads(response.text)['向量编码'] 14 | else: 15 | raise Exception('失败') 16 | 17 | def 对知识批量进行向量编码(request): 18 | 未编码的知识list = 销售入账记录.objects.filter(客户向量编码__isnull=True) 19 | for current in 未编码的知识list: 20 | current.客户向量编码= 调用向量编码服务(current.客户) 21 | current.save() 22 | 23 | result = {'code':200} 24 | return JsonResponse(result) 25 | 26 | @csrf_exempt 27 | def 导入数据(request): 28 | if request.method == "POST": 29 | raw_data = request.body.decode("utf-8") 30 | json_dict = json.loads(raw_data) 31 | 32 | 知识主表记录 = 知识主表() 33 | 知识主表记录.id = json_dict["知识主表id"] 34 | 知识主表记录.模块 = json_dict["模块"] 35 | 知识主表记录.标题 = json_dict["标题"] 36 | 知识主表记录.url = json_dict["url"] 37 | 知识主表记录.向量编码 = 调用向量编码服务(知识主表记录.标题) 38 | 知识主表记录.save() 39 | 40 | 知识详细表记录 = 知识详细表() 41 | 知识详细表记录.id = json_dict["知识详细表记录id"] 42 | 知识详细表记录.文本内容 = json_dict["文本内容"] 43 | 知识详细表记录.内容在分段中的顺序 = json_dict["内容在分段中的顺序"] 44 | 知识详细表记录.向量编码 = 调用向量编码服务(知识详细表记录.文本内容) 45 | 知识详细表记录.知识主表 = 知识主表记录 46 | 知识详细表记录.save() 47 | 48 | result = {'文本内容':json_dict["文本内容"]} 49 | return JsonResponse(result,json_dumps_params={'ensure_ascii': False}) -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/api.py: -------------------------------------------------------------------------------- 1 | import json 2 | from django.http import JsonResponse 3 | from django.views.decorators.csrf import csrf_exempt 4 | import requests 5 | 6 | from .models import 知识主表, 知识详细表, 销售入账记录 7 | def 调用向量编码服务(输入字符串): 8 | body_json = { 9 | "input":输入字符串 10 | } 11 | response = requests.post(f"http://127.0.0.1:8902/api/embedding/encode", json=body_json) 12 | if response.status_code == 200: 13 | return json.loads(response.text)['向量编码'] 14 | else: 15 | raise Exception('失败') 16 | 17 | def 对知识批量进行向量编码(request): 18 | 未编码的知识list = 销售入账记录.objects.filter(客户向量编码__isnull=True) 19 | for current in 未编码的知识list: 20 | current.客户向量编码= 调用向量编码服务(current.客户) 21 | current.save() 22 | 23 | result = {'code':200} 24 | return JsonResponse(result) 25 | 26 | @csrf_exempt 27 | def 导入数据(request): 28 | if request.method == "POST": 29 | raw_data = request.body.decode("utf-8") 30 | json_dict = json.loads(raw_data) 31 | 32 | 知识主表记录 = 知识主表() 33 | 知识主表记录.id = json_dict["知识主表id"] 34 | 知识主表记录.模块 = json_dict["模块"] 35 | 知识主表记录.标题 = json_dict["标题"] 36 | 知识主表记录.url = json_dict["url"] 37 | 知识主表记录.向量编码 = 调用向量编码服务(知识主表记录.标题) 38 | 知识主表记录.save() 39 | 40 | 知识详细表记录 = 知识详细表() 41 | 知识详细表记录.id = json_dict["知识详细表记录id"] 42 | 知识详细表记录.文本内容 = json_dict["文本内容"] 43 | 知识详细表记录.内容在分段中的顺序 = json_dict["内容在分段中的顺序"] 44 | 知识详细表记录.向量编码 = 调用向量编码服务(知识详细表记录.文本内容) 45 | 知识详细表记录.知识主表 = 知识主表记录 46 | 知识详细表记录.save() 47 | 48 | result = {'文本内容':json_dict["文本内容"]} 49 | return JsonResponse(result,json_dumps_params={'ensure_ascii': False}) -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/api.py: -------------------------------------------------------------------------------- 1 | import json 2 | from django.http import JsonResponse 3 | from django.views.decorators.csrf import csrf_exempt 4 | import requests 5 | 6 | from .models import 知识主表, 知识详细表, 销售入账记录 7 | def 调用向量编码服务(输入字符串): 8 | body_json = { 9 | "input":输入字符串 10 | } 11 | response = requests.post(f"http://127.0.0.1:8902/api/embedding/encode", json=body_json) 12 | if response.status_code == 200: 13 | return json.loads(response.text)['向量编码'] 14 | else: 15 | raise Exception('失败') 16 | 17 | def 对知识批量进行向量编码(request): 18 | 未编码的知识list = 销售入账记录.objects.filter(客户向量编码__isnull=True) 19 | for current in 未编码的知识list: 20 | current.客户向量编码= 调用向量编码服务(current.客户) 21 | current.save() 22 | 23 | result = {'code':200} 24 | return JsonResponse(result) 25 | 26 | @csrf_exempt 27 | def 导入数据(request): 28 | if request.method == "POST": 29 | raw_data = request.body.decode("utf-8") 30 | json_dict = json.loads(raw_data) 31 | 32 | 知识主表记录 = 知识主表() 33 | 知识主表记录.id = json_dict["知识主表id"] 34 | 知识主表记录.模块 = json_dict["模块"] 35 | 知识主表记录.标题 = json_dict["标题"] 36 | 知识主表记录.url = json_dict["url"] 37 | 知识主表记录.向量编码 = 调用向量编码服务(知识主表记录.标题) 38 | 知识主表记录.save() 39 | 40 | 知识详细表记录 = 知识详细表() 41 | 知识详细表记录.id = json_dict["知识详细表记录id"] 42 | 知识详细表记录.文本内容 = json_dict["文本内容"] 43 | 知识详细表记录.内容在分段中的顺序 = json_dict["内容在分段中的顺序"] 44 | 知识详细表记录.向量编码 = 调用向量编码服务(知识详细表记录.文本内容) 45 | 知识详细表记录.知识主表 = 知识主表记录 46 | 知识详细表记录.save() 47 | 48 | result = {'文本内容':json_dict["文本内容"]} 49 | return JsonResponse(result,json_dumps_params={'ensure_ascii': False}) -------------------------------------------------------------------------------- /实战案例2/rag.py: -------------------------------------------------------------------------------- 1 | import json 2 | import os 3 | 4 | import requests 5 | 6 | #region 跟具体大模型相关的,如果需要修改大模型,可能需要修改这部分函数 7 | 文本划分长度 = 1500 8 | 9 | def get_access_token(): 10 | ernie_client_id = os.getenv("baiduclientid") 11 | ernie_client_secret = os.getenv("baiduclientsecret") 12 | url = f"https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={ernie_client_id}&client_secret={ernie_client_secret}" 13 | 14 | playload = json.dumps("") 15 | headers = { 16 | 'Content-Type': 'application/json', 17 | 'Accept': 'application/json' 18 | } 19 | response = requests.request("POST", url, headers=headers, data=playload) 20 | return response.json().get("access_token") 21 | 22 | def 对话模式(messages): 23 | url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-lite-8k?access_token=" + get_access_token() 24 | 25 | json_obj = { 26 | "messages": messages, 27 | } 28 | 29 | playload = json.dumps(json_obj) 30 | headers = { 31 | 'Content-Type': 'application/json' 32 | } 33 | 34 | response = requests.request("POST", url, headers=headers, data=playload) 35 | json_result = json.loads(response.text) 36 | if "error_code" in json_result: 37 | return json_result["error_msg"] + ":" + playload 38 | else: 39 | return json_result["result"] 40 | #endregion 41 | 42 | #region 构造messages相关 43 | def 构造文本摘要messages(输入字符串): 44 | messages=[ 45 | {"role": "user", "content": f""" 46 | 请对以下文本进行摘要: 47 | 48 | {输入字符串} 49 | """}, 50 | ] 51 | return messages 52 | 53 | def 构造英译中messages(输入字符串): 54 | messages=[ 55 | {"role": "user", "content": f""" 56 | 请将以下文本翻译成中文: 57 | 58 | {输入字符串} 59 | """}, 60 | ] 61 | return messages 62 | 63 | #endregion 64 | -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/templates/home/index.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block main_content%} 4 |
5 |

首页

6 |
7 | 8 |
9 | {% if question is not None %} 10 |
11 | 你:{{question}} 12 |
13 | {% endif %} 14 | {% for current_obj in object_list %} 15 | {% if current_obj.不带入大模型对话中 is False%} 16 |
17 | {% if current_obj.role == "user" %} 18 | 你:{{current_obj.content}} 19 |
20 | {% else %} 21 | AI:{{current_obj.处理后content}} 22 | {% if link_text is not None %} 23 |
24 | 参考链接:{{link_text}}   25 |
26 |
27 | 好评 28 |   29 | 差评 30 |
31 | {% endif %} 32 |
33 | {% endif %} 34 |
35 | {% endif %} 36 | {% endfor %} 37 | 38 |
39 | {% csrf_token %} 40 |
41 | 42 |
43 | 44 |
45 |   46 |
47 |
48 | 开始新的对话 49 |
50 | {% endblock %} -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/home/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | from pgvector.django import VectorField 3 | 4 | class 销售入账记录(models.Model): 5 | id = models.AutoField(primary_key=True) 6 | 7 | 客户 = models.CharField(max_length=255) 8 | 客户向量编码 = VectorField(dimensions=1024,null=True,blank=True) 9 | 客户向量编码模型 = models.TextField(default="bge-large-zh-v1.5") 10 | 入账日期 = models.DateTimeField() 11 | 入账金额 = models.TextField(null=True) 12 | 已到账款项 = models.IntegerField(null=True) 13 | 剩余到账款项 = models.IntegerField(null=True) 14 | 15 | class 对话记录(models.Model): 16 | id = models.AutoField(primary_key=True) 17 | 18 | role = models.TextField() 19 | content = models.TextField(null=True) 20 | 处理后content = models.TextField(null=True) 21 | 提交给大模型的playload = models.TextField(null=True) 22 | 不带入大模型对话中 = models.BooleanField(default=False) 23 | 已结束 = models.BooleanField(default=False) 24 | 25 | created_time = models.DateTimeField(auto_now_add=True) 26 | lastmodified_time = models.DateTimeField(auto_now=True) 27 | 28 | class 知识主表(models.Model): 29 | id = models.IntegerField( 30 | primary_key = True, 31 | editable = False) 32 | 33 | 模块 = models.TextField(null=True,blank=True) 34 | 标题 = models.TextField(null=True,blank=True) 35 | url = models.TextField(null=True,blank=True) 36 | 向量编码 = VectorField(dimensions=1024,null=True,blank=True) 37 | 向量编码模型 = models.TextField(default="bge-large-zh-v1.5") 38 | 39 | class 知识详细表(models.Model): 40 | id = models.IntegerField( 41 | primary_key = True, 42 | editable = False) 43 | 44 | 文本内容 = models.TextField() 45 | 内容在分段中的顺序 = models.IntegerField(null=True,blank=True) 46 | 向量编码 = VectorField(dimensions=1024,null=True,blank=True) 47 | 向量编码模型 = models.TextField(default="bge-large-zh-v1.5") 48 | 知识主表 = models.ForeignKey(知识主表, on_delete=models.CASCADE) -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/home/migrations/0009_知识主表_知识详细表.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-11-11 02:33 2 | 3 | from django.db import migrations, models 4 | import django.db.models.deletion 5 | import pgvector.django.vector 6 | 7 | 8 | class Migration(migrations.Migration): 9 | 10 | dependencies = [ 11 | ('home', '0008_alter_对话记录_id_alter_销售入账记录_id'), 12 | ] 13 | 14 | operations = [ 15 | migrations.CreateModel( 16 | name='知识主表', 17 | fields=[ 18 | ('id', models.IntegerField(editable=False, primary_key=True, serialize=False)), 19 | ('created_time', models.DateTimeField(auto_now_add=True)), 20 | ('lastmodified_time', models.DateTimeField(auto_now=True)), 21 | ('标题', models.TextField()), 22 | ('url', models.TextField()), 23 | ('创建时间', models.DateTimeField(blank=True, null=True)), 24 | ('作者', models.TextField(blank=True, null=True)), 25 | ('权限', models.TextField(blank=True, null=True)), 26 | ('向量编码', pgvector.django.vector.VectorField(blank=True, dimensions=1024, null=True)), 27 | ('向量编码模型', models.TextField(default='bge-large-zh-v1.5')), 28 | ], 29 | ), 30 | migrations.CreateModel( 31 | name='知识详细表', 32 | fields=[ 33 | ('id', models.IntegerField(editable=False, primary_key=True, serialize=False)), 34 | ('created_time', models.DateTimeField(auto_now_add=True)), 35 | ('lastmodified_time', models.DateTimeField(auto_now=True)), 36 | ('文本内容', models.TextField()), 37 | ('向量编码', pgvector.django.vector.VectorField(blank=True, dimensions=1024, null=True)), 38 | ('向量编码模型', models.TextField(default='bge-large-zh-v1.5')), 39 | ('知识主表', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='home.知识主表')), 40 | ], 41 | ), 42 | ] 43 | -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/migrations/0009_知识主表_知识详细表.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-11-11 02:33 2 | 3 | from django.db import migrations, models 4 | import django.db.models.deletion 5 | import pgvector.django.vector 6 | 7 | 8 | class Migration(migrations.Migration): 9 | 10 | dependencies = [ 11 | ('home', '0008_alter_对话记录_id_alter_销售入账记录_id'), 12 | ] 13 | 14 | operations = [ 15 | migrations.CreateModel( 16 | name='知识主表', 17 | fields=[ 18 | ('id', models.IntegerField(editable=False, primary_key=True, serialize=False)), 19 | ('created_time', models.DateTimeField(auto_now_add=True)), 20 | ('lastmodified_time', models.DateTimeField(auto_now=True)), 21 | ('标题', models.TextField()), 22 | ('url', models.TextField()), 23 | ('创建时间', models.DateTimeField(blank=True, null=True)), 24 | ('作者', models.TextField(blank=True, null=True)), 25 | ('权限', models.TextField(blank=True, null=True)), 26 | ('向量编码', pgvector.django.vector.VectorField(blank=True, dimensions=1024, null=True)), 27 | ('向量编码模型', models.TextField(default='bge-large-zh-v1.5')), 28 | ], 29 | ), 30 | migrations.CreateModel( 31 | name='知识详细表', 32 | fields=[ 33 | ('id', models.IntegerField(editable=False, primary_key=True, serialize=False)), 34 | ('created_time', models.DateTimeField(auto_now_add=True)), 35 | ('lastmodified_time', models.DateTimeField(auto_now=True)), 36 | ('文本内容', models.TextField()), 37 | ('向量编码', pgvector.django.vector.VectorField(blank=True, dimensions=1024, null=True)), 38 | ('向量编码模型', models.TextField(default='bge-large-zh-v1.5')), 39 | ('知识主表', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='home.知识主表')), 40 | ], 41 | ), 42 | ] 43 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/migrations/0009_知识主表_知识详细表.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.2.13 on 2024-11-11 02:33 2 | 3 | from django.db import migrations, models 4 | import django.db.models.deletion 5 | import pgvector.django.vector 6 | 7 | 8 | class Migration(migrations.Migration): 9 | 10 | dependencies = [ 11 | ('home', '0008_alter_对话记录_id_alter_销售入账记录_id'), 12 | ] 13 | 14 | operations = [ 15 | migrations.CreateModel( 16 | name='知识主表', 17 | fields=[ 18 | ('id', models.IntegerField(editable=False, primary_key=True, serialize=False)), 19 | ('created_time', models.DateTimeField(auto_now_add=True)), 20 | ('lastmodified_time', models.DateTimeField(auto_now=True)), 21 | ('标题', models.TextField()), 22 | ('url', models.TextField()), 23 | ('创建时间', models.DateTimeField(blank=True, null=True)), 24 | ('作者', models.TextField(blank=True, null=True)), 25 | ('权限', models.TextField(blank=True, null=True)), 26 | ('向量编码', pgvector.django.vector.VectorField(blank=True, dimensions=1024, null=True)), 27 | ('向量编码模型', models.TextField(default='bge-large-zh-v1.5')), 28 | ], 29 | ), 30 | migrations.CreateModel( 31 | name='知识详细表', 32 | fields=[ 33 | ('id', models.IntegerField(editable=False, primary_key=True, serialize=False)), 34 | ('created_time', models.DateTimeField(auto_now_add=True)), 35 | ('lastmodified_time', models.DateTimeField(auto_now=True)), 36 | ('文本内容', models.TextField()), 37 | ('向量编码', pgvector.django.vector.VectorField(blank=True, dimensions=1024, null=True)), 38 | ('向量编码模型', models.TextField(default='bge-large-zh-v1.5')), 39 | ('知识主表', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='home.知识主表')), 40 | ], 41 | ), 42 | ] 43 | -------------------------------------------------------------------------------- /实战案例1/改造前/templates/home/addsalescheck.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block main_content%} 4 |
5 |

销售管理 / 销售对账 / 添加入账记录

6 |
7 | 10 |
11 |
12 | 13 |
14 |
15 | {% csrf_token %} 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 | {% endblock %} -------------------------------------------------------------------------------- /实战案例1/改造后/home/views.py: -------------------------------------------------------------------------------- 1 | import json 2 | from django.http import HttpResponse 3 | from django.shortcuts import redirect, render 4 | from django.urls import reverse 5 | 6 | from .rag import * 7 | 8 | from .search import 查询 9 | 10 | from .models import 对话记录, 销售入账记录 11 | 12 | from django.core import serializers 13 | 14 | def newtalk(request): 15 | 开始新的对话() 16 | return redirect(reverse('home:index')) 17 | 18 | def index(request): 19 | if request.method == 'POST': 20 | 用户输入 = request.POST['question'] 21 | 22 | 查询参数 = 获取结构化数据查询参数(用户输入) 23 | 查询结果 = None 24 | if 查询参数 is not None: 25 | 查询结果 = 查询(查询参数) 26 | print(f'查询结果={json.dumps(查询结果)}') 27 | 28 | if 查询结果 is None: 29 | 从数据库查不到相关数据时的操作() 30 | else: 31 | 查询结果json格式 = serializers.serialize("json", list(查询结果)) 32 | 根据查询结果回答用户输入(查询结果json格式,用户输入) 33 | 34 | conversation_list = 获取当前对话记录() 35 | return render(request, "home/index.html",{"object_list":conversation_list}) 36 | 37 | def salescheck(request): 38 | # return HttpResponse("home index") 39 | if request.method == 'POST': 40 | 客户 = request.POST['name'] 41 | if 客户 is not None or 客户.strip() != '': 42 | object_list = 查询({'模块':'销售对账','客户名称':客户}) 43 | else: 44 | object_list = 销售入账记录.objects.all() 45 | else: 46 | object_list = 销售入账记录.objects.all() 47 | 48 | return render(request, "home/salescheck.html",context={"object_list":object_list}) 49 | 50 | def addsalescheck(request): 51 | if request.method == 'POST': 52 | record = 销售入账记录() 53 | record.客户 = request.POST['name'] 54 | record.入账日期 = request.POST['created_at'] 55 | record.入账金额 = request.POST['amount'] 56 | record.已到账款项 = request.POST['total'] 57 | record.剩余到账款项 = request.POST['leave'] 58 | 59 | record.save() 60 | return redirect(reverse('home:salescheck')) 61 | else: 62 | return render(request, "home/addsalescheck.html") 63 | -------------------------------------------------------------------------------- /实战案例1/改造后/templates/home/addsalescheck.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block main_content%} 4 |
5 |

销售管理 / 销售对账 / 添加入账记录

6 |
7 | 10 |
11 |
12 | 13 |
14 |
15 | {% csrf_token %} 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 | {% endblock %} -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/home/views.py: -------------------------------------------------------------------------------- 1 | import json 2 | from django.http import HttpResponse 3 | from django.shortcuts import redirect, render 4 | from django.urls import reverse 5 | 6 | from .rag import * 7 | 8 | from .search import 查询 9 | 10 | from .models import 对话记录, 销售入账记录 11 | 12 | from django.core import serializers 13 | 14 | def newtalk(request): 15 | 开始新的对话() 16 | return redirect(reverse('home:index')) 17 | 18 | def index(request): 19 | 查询结果 = None 20 | if request.method == 'POST': 21 | 用户输入 = request.POST['question'] 22 | 23 | 查询结果 = 查询(用户输入) 24 | 25 | 查询结果json格式 = '{"知识内容": "' + 查询结果["知识内容"] + '"}' 26 | 27 | 根据查询结果回答用户输入(查询结果json格式,用户输入) 28 | 29 | conversation_list = 获取当前对话记录() 30 | 31 | if 查询结果 is None: 32 | return render(request, "home/index.html",{"object_list":conversation_list}) 33 | else: 34 | return render(request, "home/index.html",{"object_list":conversation_list,"question":用户输入,"link_text":查询结果["标题"],"link_url":查询结果["url"]}) 35 | 36 | def salescheck(request): 37 | # return HttpResponse("home index") 38 | if request.method == 'POST': 39 | 客户 = request.POST['name'] 40 | if 客户 is not None or 客户.strip() != '': 41 | object_list = 查询({'模块':'销售对账','客户名称':客户}) 42 | else: 43 | object_list = 销售入账记录.objects.all() 44 | else: 45 | object_list = 销售入账记录.objects.all() 46 | 47 | return render(request, "home/salescheck.html",context={"object_list":object_list}) 48 | 49 | def addsalescheck(request): 50 | if request.method == 'POST': 51 | record = 销售入账记录() 52 | record.客户 = request.POST['name'] 53 | record.入账日期 = request.POST['created_at'] 54 | record.入账金额 = request.POST['amount'] 55 | record.已到账款项 = request.POST['total'] 56 | record.剩余到账款项 = request.POST['leave'] 57 | 58 | record.save() 59 | return redirect(reverse('home:salescheck')) 60 | else: 61 | return render(request, "home/addsalescheck.html") 62 | -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/templates/home/addsalescheck.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block main_content%} 4 |
5 |

销售管理 / 销售对账 / 添加入账记录

6 |
7 | 10 |
11 |
12 | 13 |
14 |
15 | {% csrf_token %} 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 | {% endblock %} -------------------------------------------------------------------------------- /实战案例3/第18节/main/templates/home/addsalescheck.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block main_content%} 4 |
5 |

销售管理 / 销售对账 / 添加入账记录

6 |
7 | 10 |
11 |
12 | 13 |
14 |
15 | {% csrf_token %} 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 | {% endblock %} -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/templates/home/addsalescheck.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block main_content%} 4 |
5 |

销售管理 / 销售对账 / 添加入账记录

6 |
7 | 10 |
11 |
12 | 13 |
14 |
15 | {% csrf_token %} 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 | {% endblock %} -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/templates/home/addsalescheck.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block main_content%} 4 |
5 |

销售管理 / 销售对账 / 添加入账记录

6 |
7 | 10 |
11 |
12 | 13 |
14 |
15 | {% csrf_token %} 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 | {% endblock %} -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/templates/home/addsalescheck.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block main_content%} 4 |
5 |

销售管理 / 销售对账 / 添加入账记录

6 |
7 | 10 |
11 |
12 | 13 |
14 |
15 | {% csrf_token %} 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 | {% endblock %} -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | from pgvector.django import VectorField 3 | 4 | class 销售入账记录(models.Model): 5 | id = models.AutoField(primary_key=True) 6 | 7 | 客户 = models.CharField(max_length=255) 8 | 客户向量编码 = VectorField(dimensions=1024,null=True,blank=True) 9 | 客户向量编码模型 = models.TextField(default="bge-large-zh-v1.5") 10 | 入账日期 = models.DateTimeField() 11 | 入账金额 = models.TextField(null=True) 12 | 已到账款项 = models.IntegerField(null=True) 13 | 剩余到账款项 = models.IntegerField(null=True) 14 | 15 | class 对话记录(models.Model): 16 | id = models.AutoField(primary_key=True) 17 | 18 | role = models.TextField() 19 | content = models.TextField(null=True) 20 | 处理后content = models.TextField(null=True) 21 | 提交给大模型的playload = models.TextField(null=True) 22 | 不带入大模型对话中 = models.BooleanField(default=False) 23 | 已结束 = models.BooleanField(default=False) 24 | positive_review = models.BooleanField(default=False, verbose_name='好评') 25 | negative_review = models.BooleanField(default=False, verbose_name='差评') 26 | 27 | created_time = models.DateTimeField(auto_now_add=True) 28 | lastmodified_time = models.DateTimeField(auto_now=True) 29 | 30 | class 知识主表(models.Model): 31 | id = models.IntegerField( 32 | primary_key = True, 33 | editable = False) 34 | 35 | 模块 = models.TextField(null=True,blank=True) 36 | 标题 = models.TextField(null=True,blank=True) 37 | url = models.TextField(null=True,blank=True) 38 | 向量编码 = VectorField(dimensions=1024,null=True,blank=True) 39 | 向量编码模型 = models.TextField(default="bge-large-zh-v1.5") 40 | 41 | class 知识详细表(models.Model): 42 | id = models.IntegerField( 43 | primary_key = True, 44 | editable = False) 45 | 46 | 文本内容 = models.TextField() 47 | 内容在分段中的顺序 = models.IntegerField(null=True,blank=True) 48 | 向量编码 = VectorField(dimensions=1024,null=True,blank=True) 49 | 向量编码模型 = models.TextField(default="bge-large-zh-v1.5") 50 | 知识主表 = models.ForeignKey(知识主表, on_delete=models.CASCADE) -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/home/views_api.py: -------------------------------------------------------------------------------- 1 | from django.http import JsonResponse 2 | from django.views.decorators.csrf import csrf_exempt 3 | from django.core import serializers 4 | 5 | from .models import CNET新闻 6 | 7 | from .rag import * 8 | def 开始新的对话api(request): 9 | 开始新的对话() 10 | return JsonResponse({"code":200,"message":"已经成功开始新的对话"}) 11 | 12 | @csrf_exempt 13 | def 获取结构化数据查询参数api(request): 14 | 用户输入 = request.POST['question'] 15 | 16 | 查询参数 = 获取结构化数据查询参数(用户输入) 17 | return JsonResponse({"querydata":查询参数}) 18 | 19 | def 从数据库查不到相关数据时的操作api(request): 20 | 从数据库查不到相关数据时的操作() 21 | return JsonResponse({"code":200,"message":"已经成功执行从数据库查不到相关数据时的操作"}) 22 | 23 | @csrf_exempt 24 | def 根据查询结果回答用户输入api(request): 25 | 用户输入 = request.POST['question'] 26 | 查询结果 = request.POST['query-result'] 27 | 28 | 根据查询结果回答用户输入(查询结果,用户输入) 29 | return JsonResponse({"code":200,"message":"已经成功根据查询结果回答用户输入"}) 30 | 31 | def 获取对话记录api(request): 32 | conversation_list = 获取当前对话记录() 33 | conversation_list_json = serializers.serialize("json", list(conversation_list)) 34 | return JsonResponse({"conversationlist":conversation_list_json}) 35 | 36 | @csrf_exempt 37 | def CNET新闻入库api(request): 38 | # 读取request的json数据 39 | json_str = request.body.decode("utf-8") 40 | json_obj = json.loads(json_str) 41 | 42 | # 遍历json数据,将数据写入数据库 43 | for item in json_obj: 44 | CNET新闻实例 = CNET新闻() 45 | CNET新闻实例.标题 = item['元数据']['标题'] 46 | CNET新闻实例.标题中文翻译 = item['元数据']['标题_中文翻译'] 47 | 新闻发布日期_原始数据 = item['元数据']['创建日期'] 48 | 新闻发布日期=datetime(新闻发布日期_原始数据[0],新闻发布日期_原始数据[1],新闻发布日期_原始数据[2]) 49 | CNET新闻实例.新闻发布日期 = 新闻发布日期 50 | CNET新闻实例.url = item['元数据']['url'] 51 | CNET新闻实例.作者 = item['元数据']['作者'] 52 | CNET新闻实例.权限 = "CNETNews" 53 | CNET新闻实例.新闻内容 = item['新闻内容'] 54 | CNET新闻实例.摘要 = item['摘要'] 55 | CNET新闻实例.新闻内容中文翻译 = item['新闻内容_中文翻译'] 56 | CNET新闻实例.save() 57 | 58 | return JsonResponse({"code":200,"message":"已经成功将CNET新闻入库"}, 59 | json_dumps_params={'ensure_ascii': False}) -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/home/views.py: -------------------------------------------------------------------------------- 1 | import json 2 | from django.http import HttpResponse 3 | from django.shortcuts import redirect, render 4 | from django.urls import reverse 5 | 6 | from .rag import * 7 | 8 | from .search import 查询 9 | 10 | from .models import 对话记录, 销售入账记录 11 | 12 | from django.core import serializers 13 | 14 | def newtalk(request): 15 | 开始新的对话() 16 | return redirect(reverse('home:index')) 17 | 18 | def index(request): 19 | if request.method == 'POST': 20 | 用户输入 = request.POST['question'] 21 | 22 | 查询参数 = 获取结构化数据查询参数(用户输入) 23 | 查询结果 = None 24 | if 查询参数 is not None: 25 | 查询结果 = 查询(查询参数) 26 | # print(f'查询结果={json.dumps(查询结果)}') 27 | 28 | if 查询结果 is None: 29 | 从数据库查不到相关数据时的操作() 30 | else: 31 | try: 32 | 查询结果json格式 = serializers.serialize("json", list(查询结果)) 33 | except: 34 | 查询结果json格式 = json.dumps(list(查询结果), ensure_ascii=False) 35 | 根据查询结果回答用户输入(查询结果json格式,用户输入) 36 | 37 | conversation_list = 获取当前对话记录() 38 | return render(request, "home/index.html",{"object_list":conversation_list}) 39 | 40 | def salescheck(request): 41 | # return HttpResponse("home index") 42 | if request.method == 'POST': 43 | 客户 = request.POST['name'] 44 | if 客户 is not None or 客户.strip() != '': 45 | object_list = 查询({'模块':'销售对账','客户名称':客户}) 46 | else: 47 | object_list = 销售入账记录.objects.all() 48 | else: 49 | object_list = 销售入账记录.objects.all() 50 | 51 | return render(request, "home/salescheck.html",context={"object_list":object_list}) 52 | 53 | def addsalescheck(request): 54 | if request.method == 'POST': 55 | record = 销售入账记录() 56 | record.客户 = request.POST['name'] 57 | record.入账日期 = request.POST['created_at'] 58 | record.入账金额 = request.POST['amount'] 59 | record.已到账款项 = request.POST['total'] 60 | record.剩余到账款项 = request.POST['leave'] 61 | 62 | record.save() 63 | return redirect(reverse('home:salescheck')) 64 | else: 65 | return render(request, "home/addsalescheck.html") 66 | -------------------------------------------------------------------------------- /实战案例1/改造前/templates/home/salescheck.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block main_content%} 4 |
5 |

销售管理 / 销售对账

6 |
7 |
8 | 添加记录 9 |
10 |
11 |
12 | 13 |
14 |
15 | {% csrf_token %} 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 | {% for current_obj in object_list %} 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | {% empty %} 51 | 52 | 56 | 57 | {% endfor %} 58 | 59 |
#操作客户入账日期入账金额已到账款项剩余到账款项
{{current_obj.id}}修改   删除{{current_obj.客户}}{{current_obj.入账日期}}{{current_obj.入账金额}}{{current_obj.已到账款项}}{{current_obj.剩余到账款项}}
53 |
暂无数据 54 |
55 |
60 |
61 | {% endblock %} -------------------------------------------------------------------------------- /实战案例1/改造后/templates/home/salescheck.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block main_content%} 4 |
5 |

销售管理 / 销售对账

6 |
7 |
8 | 添加记录 9 |
10 |
11 |
12 | 13 |
14 |
15 | {% csrf_token %} 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 | {% for current_obj in object_list %} 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | {% empty %} 51 | 52 | 56 | 57 | {% endfor %} 58 | 59 |
#操作客户入账日期入账金额已到账款项剩余到账款项
{{current_obj.id}}修改   删除{{current_obj.客户}}{{current_obj.入账日期}}{{current_obj.入账金额}}{{current_obj.已到账款项}}{{current_obj.剩余到账款项}}
53 |
暂无数据 54 |
55 |
60 |
61 | {% endblock %} -------------------------------------------------------------------------------- /实战案例3/第18节/main/home/views.py: -------------------------------------------------------------------------------- 1 | import json 2 | from django.http import HttpResponse 3 | from django.shortcuts import redirect, render 4 | from django.urls import reverse 5 | 6 | from .rag import * 7 | 8 | from .search import 查询 9 | 10 | from .models import 对话记录, 销售入账记录 11 | 12 | from django.core import serializers 13 | 14 | def newtalk(request): 15 | 开始新的对话() 16 | return redirect(reverse('home:index')) 17 | 18 | def index(request): 19 | if request.method == 'POST': 20 | 用户输入 = request.POST['question'] 21 | 22 | 查询参数 = 获取结构化数据查询参数(用户输入) 23 | 查询结果 = None 24 | if 查询参数 is not None: 25 | 查询结果 = 查询(查询参数) 26 | print(f'查询结果={json.dumps(查询结果)}') 27 | 28 | if 查询结果 is None: 29 | 从数据库查不到相关数据时的操作() 30 | else: 31 | if isinstance(查询结果, list): 32 | 查询结果json格式 = json.dumps(查询结果) 33 | else: 34 | 查询结果json格式 = serializers.serialize("json", list(查询结果)) 35 | 36 | 根据查询结果回答用户输入(查询结果json格式,用户输入) 37 | 38 | conversation_list = 获取当前对话记录() 39 | return render(request, "home/index.html",{"object_list":conversation_list}) 40 | 41 | def salescheck(request): 42 | # return HttpResponse("home index") 43 | if request.method == 'POST': 44 | 客户 = request.POST['name'] 45 | if 客户 is not None or 客户.strip() != '': 46 | object_list = 查询({'模块':'销售对账','客户名称':客户}) 47 | else: 48 | object_list = 销售入账记录.objects.all() 49 | else: 50 | object_list = 销售入账记录.objects.all() 51 | 52 | return render(request, "home/salescheck.html",context={"object_list":object_list}) 53 | 54 | def addsalescheck(request): 55 | if request.method == 'POST': 56 | record = 销售入账记录() 57 | record.客户 = request.POST['name'] 58 | record.入账日期 = request.POST['created_at'] 59 | record.入账金额 = request.POST['amount'] 60 | record.已到账款项 = request.POST['total'] 61 | record.剩余到账款项 = request.POST['leave'] 62 | 63 | record.save() 64 | return redirect(reverse('home:salescheck')) 65 | else: 66 | return render(request, "home/addsalescheck.html") 67 | -------------------------------------------------------------------------------- /实战案例3/第18节/main/templates/home/salescheck.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block main_content%} 4 |
5 |

销售管理 / 销售对账

6 |
7 |
8 | 添加记录 9 |
10 |
11 |
12 | 13 |
14 |
15 | {% csrf_token %} 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 | {% for current_obj in object_list %} 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | {% empty %} 51 | 52 | 56 | 57 | {% endfor %} 58 | 59 |
#操作客户入账日期入账金额已到账款项剩余到账款项
{{current_obj.id}}修改   删除{{current_obj.客户}}{{current_obj.入账日期}}{{current_obj.入账金额}}{{current_obj.已到账款项}}{{current_obj.剩余到账款项}}
53 |
暂无数据 54 |
55 |
60 |
61 | {% endblock %} -------------------------------------------------------------------------------- /实战案例2/整合了案例2的实战案例1/templates/home/salescheck.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block main_content%} 4 |
5 |

销售管理 / 销售对账

6 |
7 |
8 | 添加记录 9 |
10 |
11 |
12 | 13 |
14 |
15 | {% csrf_token %} 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 | {% for current_obj in object_list %} 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | {% empty %} 51 | 52 | 56 | 57 | {% endfor %} 58 | 59 |
#操作客户入账日期入账金额已到账款项剩余到账款项
{{current_obj.id}}修改   删除{{current_obj.客户}}{{current_obj.入账日期}}{{current_obj.入账金额}}{{current_obj.已到账款项}}{{current_obj.剩余到账款项}}
53 |
暂无数据 54 |
55 |
60 |
61 | {% endblock %} -------------------------------------------------------------------------------- /实战案例3/第19节/程序/main/templates/home/salescheck.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block main_content%} 4 |
5 |

销售管理 / 销售对账

6 |
7 |
8 | 添加记录 9 |
10 |
11 |
12 | 13 |
14 |
15 | {% csrf_token %} 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 | {% for current_obj in object_list %} 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | {% empty %} 51 | 52 | 56 | 57 | {% endfor %} 58 | 59 |
#操作客户入账日期入账金额已到账款项剩余到账款项
{{current_obj.id}}修改   删除{{current_obj.客户}}{{current_obj.入账日期}}{{current_obj.入账金额}}{{current_obj.已到账款项}}{{current_obj.剩余到账款项}}
53 |
暂无数据 54 |
55 |
60 |
61 | {% endblock %} -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/templates/home/salescheck.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block main_content%} 4 |
5 |

销售管理 / 销售对账

6 |
7 |
8 | 添加记录 9 |
10 |
11 |
12 | 13 |
14 |
15 | {% csrf_token %} 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 | {% for current_obj in object_list %} 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | {% empty %} 51 | 52 | 56 | 57 | {% endfor %} 58 | 59 |
#操作客户入账日期入账金额已到账款项剩余到账款项
{{current_obj.id}}修改   删除{{current_obj.客户}}{{current_obj.入账日期}}{{current_obj.入账金额}}{{current_obj.已到账款项}}{{current_obj.剩余到账款项}}
53 |
暂无数据 54 |
55 |
60 |
61 | {% endblock %} -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/templates/home/salescheck.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block main_content%} 4 |
5 |

销售管理 / 销售对账

6 |
7 |
8 | 添加记录 9 |
10 |
11 |
12 | 13 |
14 |
15 | {% csrf_token %} 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 | {% for current_obj in object_list %} 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | {% empty %} 51 | 52 | 56 | 57 | {% endfor %} 58 | 59 |
#操作客户入账日期入账金额已到账款项剩余到账款项
{{current_obj.id}}修改   删除{{current_obj.客户}}{{current_obj.入账日期}}{{current_obj.入账金额}}{{current_obj.已到账款项}}{{current_obj.剩余到账款项}}
53 |
暂无数据 54 |
55 |
60 |
61 | {% endblock %} -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | from pgvector.django import VectorField 3 | 4 | class 销售入账记录(models.Model): 5 | id = models.AutoField(primary_key=True) 6 | 7 | 客户 = models.CharField(max_length=255) 8 | 客户向量编码 = VectorField(dimensions=1024,null=True,blank=True) 9 | 客户向量编码模型 = models.TextField(default="bge-large-zh-v1.5") 10 | 入账日期 = models.DateTimeField() 11 | 入账金额 = models.TextField(null=True) 12 | 已到账款项 = models.IntegerField(null=True) 13 | 剩余到账款项 = models.IntegerField(null=True) 14 | 15 | class 对话记录(models.Model): 16 | id = models.AutoField(primary_key=True) 17 | 18 | role = models.TextField() 19 | content = models.TextField(null=True) 20 | 处理后content = models.TextField(null=True) 21 | 提交给大模型的playload = models.TextField(null=True) 22 | 不带入大模型对话中 = models.BooleanField(default=False) 23 | 已结束 = models.BooleanField(default=False) 24 | positive_review = models.BooleanField(default=False, verbose_name='好评') 25 | negative_review = models.BooleanField(default=False, verbose_name='差评') 26 | 27 | created_time = models.DateTimeField(auto_now_add=True) 28 | lastmodified_time = models.DateTimeField(auto_now=True) 29 | 30 | class 知识主表(models.Model): 31 | id = models.IntegerField( 32 | primary_key = True, 33 | editable = False) 34 | 35 | 模块 = models.TextField(null=True,blank=True) 36 | 标题 = models.TextField(null=True,blank=True) 37 | url = models.TextField(null=True,blank=True) 38 | 向量编码 = VectorField(dimensions=1024,null=True,blank=True) 39 | 向量编码模型 = models.TextField(default="bge-large-zh-v1.5") 40 | 41 | class 知识详细表(models.Model): 42 | id = models.IntegerField( 43 | primary_key = True, 44 | editable = False) 45 | 46 | 文本内容 = models.TextField() 47 | 内容在分段中的顺序 = models.IntegerField(null=True,blank=True) 48 | 向量编码 = VectorField(dimensions=1024,null=True,blank=True) 49 | 向量编码模型 = models.TextField(default="bge-large-zh-v1.5") 50 | 知识主表 = models.ForeignKey(知识主表, on_delete=models.CASCADE) 51 | 52 | class 关键词(models.Model): 53 | id = models.IntegerField( 54 | primary_key = True, 55 | editable = False) 56 | 57 | 关键词 = models.TextField() 58 | 模块 = models.TextField() 59 | 备注 = models.TextField() -------------------------------------------------------------------------------- /实战案例3/第20节/程序/main/home/views.py: -------------------------------------------------------------------------------- 1 | import json 2 | from django.http import HttpResponse 3 | from django.shortcuts import redirect, render 4 | from django.urls import reverse 5 | 6 | from .rag import * 7 | 8 | from .search import 查询 9 | 10 | from .models import 对话记录, 销售入账记录 11 | 12 | from django.core import serializers 13 | 14 | def review(request): 15 | conversationid = request.GET.get('conversationid') 16 | review = request.GET.get('review') 17 | 18 | current对话记录 = 对话记录.objects.get(id=conversationid) 19 | if review == 'positive': 20 | current对话记录.positive_review = True 21 | current对话记录.save() 22 | if review == 'negative': 23 | current对话记录.negative_review = True 24 | current对话记录.save() 25 | 26 | return redirect("http://127.0.0.1:8000/admin/home/对话记录/") 27 | 28 | def newtalk(request): 29 | 开始新的对话() 30 | return redirect(reverse('home:index')) 31 | 32 | def index(request): 33 | 查询结果 = None 34 | if request.method == 'POST': 35 | 用户输入 = request.POST['question'] 36 | 37 | 查询结果 = 查询(用户输入) 38 | 39 | 查询结果json格式 = '{"知识内容": "' + 查询结果["知识内容"] + '"}' 40 | 41 | 根据查询结果回答用户输入(查询结果json格式,用户输入) 42 | 43 | conversation_list = 获取当前对话记录() 44 | 45 | if 查询结果 is None: 46 | return render(request, "home/index.html",{"object_list":conversation_list}) 47 | else: 48 | return render(request, "home/index.html",{"object_list":conversation_list,"question":用户输入,"link_text":查询结果["标题"],"link_url":查询结果["url"]}) 49 | 50 | def salescheck(request): 51 | # return HttpResponse("home index") 52 | if request.method == 'POST': 53 | 客户 = request.POST['name'] 54 | if 客户 is not None or 客户.strip() != '': 55 | object_list = 查询({'模块':'销售对账','客户名称':客户}) 56 | else: 57 | object_list = 销售入账记录.objects.all() 58 | else: 59 | object_list = 销售入账记录.objects.all() 60 | 61 | return render(request, "home/salescheck.html",context={"object_list":object_list}) 62 | 63 | def addsalescheck(request): 64 | if request.method == 'POST': 65 | record = 销售入账记录() 66 | record.客户 = request.POST['name'] 67 | record.入账日期 = request.POST['created_at'] 68 | record.入账金额 = request.POST['amount'] 69 | record.已到账款项 = request.POST['total'] 70 | record.剩余到账款项 = request.POST['leave'] 71 | 72 | record.save() 73 | return redirect(reverse('home:salescheck')) 74 | else: 75 | return render(request, "home/addsalescheck.html") 76 | -------------------------------------------------------------------------------- /实战案例2/生成简报.py: -------------------------------------------------------------------------------- 1 | from datetime import datetime 2 | import subprocess 3 | 4 | 5 | def 生成每日简报(今天的新闻list:list): 6 | print("生成每日简报") 7 | 8 | # 获取当前日期和时间 9 | now = datetime.now() 10 | 11 | # 将其格式化为字符串,例如 "2023-04-05_14-30-00" 12 | formatted_date = now.strftime("%Y-%m-%d") 13 | 14 | # 使用日期字符串作为文件名 15 | 每日简报文件 = f"{formatted_date}.CNET每日简报.html" 16 | 17 | with open(每日简报文件, 'w', encoding='utf-8') as f: 18 | f.write(''' 19 | 20 | CNET每日简报 21 | 22 | 26 | 27 | 28 | 29 | 30 | 31 | ''') 32 | f.write(f"

CNET每日简报-{formatted_date}

") 33 | f.write('
') 34 | for current in 今天的新闻list: 35 | f.write(''' 36 |
37 |
38 |
39 |
40 |

41 | 49 |

50 |
51 |
52 |

53 | ''') 54 | f.write(current.新闻内容_中文翻译) 55 | f.write(''' 56 |

阅读原文

62 |
63 |
64 |
65 |
66 | ''') 67 | f.write("") 68 | f.write("
") 69 | 70 | return 每日简报文件 71 | def 打开每日简报(每日简报文件): 72 | # 使用默认的应用程序打开HTML文件 73 | subprocess.run(['start', 每日简报文件], shell=True, check=True) 74 | -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/templates/home/index.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block main_content%} 4 |
5 |

首页

6 |
7 | 8 |
9 | 25 | 26 | {% if question is not None %} 27 |
28 | 你:{{question}} 29 |
30 | {% endif %} 31 | {% for current_obj in object_list %} 32 | {% if current_obj.不带入大模型对话中 is False%} 33 |
34 | {% if current_obj.role == "user" %} 35 | 你:{{current_obj.content}} 36 |
37 | {% else %} 38 | AI:{{current_obj.处理后content}} 39 | {% if link_text is not None %} 40 |
41 | 参考链接:{{link_text}}   42 |
43 |
44 | 好评 45 |   46 | 差评 47 |
48 | {% endif %} 49 |
50 | {% endif %} 51 |
52 | {% endif %} 53 | {% endfor %} 54 | 55 |
56 | {% csrf_token %} 57 |
58 | 59 |
60 | 61 | 62 |
63 |   64 |
65 |
66 | 开始新的对话 67 |
68 | {% endblock %} -------------------------------------------------------------------------------- /实战案例3/第21节/程序/main/home/views.py: -------------------------------------------------------------------------------- 1 | import json 2 | from django.http import HttpResponse 3 | from django.shortcuts import redirect, render 4 | from django.urls import reverse 5 | 6 | from .rag import * 7 | 8 | from .search import 查询 9 | 10 | from .models import 关键词, 对话记录, 销售入账记录 11 | 12 | from django.core import serializers 13 | 14 | def review(request): 15 | conversationid = request.GET.get('conversationid') 16 | review = request.GET.get('review') 17 | 18 | current对话记录 = 对话记录.objects.get(id=conversationid) 19 | if review == 'positive': 20 | current对话记录.positive_review = True 21 | current对话记录.save() 22 | if review == 'negative': 23 | current对话记录.negative_review = True 24 | current对话记录.save() 25 | 26 | return redirect("http://127.0.0.1:8000/admin/home/对话记录/") 27 | 28 | def newtalk(request): 29 | 开始新的对话() 30 | return redirect(reverse('home:index')) 31 | 32 | def index(request): 33 | try: 34 | module = request.GET['module'] 35 | if module is None: 36 | module = 1 37 | else: 38 | module = int(module) 39 | except: 40 | module = 1 41 | 42 | 查询结果 = None 43 | if request.method == 'POST': 44 | 用户输入 = request.POST['question'] 45 | 46 | 关键词RawQuerySet = 关键词.objects.raw("SELECT id, 关键词, 模块, 备注 FROM public.home_关键词 where 模块='" + str(module) + "' and position(关键词 in '" + 用户输入 + "') > 0;") 47 | 48 | if 关键词RawQuerySet is not None and len(关键词RawQuerySet) > 0: 49 | if module == 1: 50 | 查询参数 = {'模块':1,'客户名称':关键词RawQuerySet[0].关键词} 51 | else: 52 | 查询参数 = 获取结构化数据查询参数(用户输入,module) 53 | 54 | 查询结果 = None 55 | if 查询参数 is not None: 56 | 查询结果 = 查询(查询参数) 57 | print(f'查询结果={json.dumps(查询结果)}') 58 | 59 | if 查询结果 is None: 60 | 从数据库查不到相关数据时的操作() 61 | else: 62 | if isinstance(查询结果, list): 63 | 查询结果json格式 = json.dumps(查询结果) 64 | else: 65 | 查询结果json格式 = serializers.serialize("json", list(查询结果)) 66 | 67 | 根据查询结果回答用户输入(查询结果json格式,用户输入) 68 | 69 | conversation_list = 获取当前对话记录() 70 | 71 | return render(request, "home/index.html",{"object_list":conversation_list,"module":module}) 72 | 73 | def salescheck(request): 74 | # return HttpResponse("home index") 75 | if request.method == 'POST': 76 | 客户 = request.POST['name'] 77 | if 客户 is not None or 客户.strip() != '': 78 | object_list = 查询({'模块':'销售对账','客户名称':客户}) 79 | else: 80 | object_list = 销售入账记录.objects.all() 81 | else: 82 | object_list = 销售入账记录.objects.all() 83 | 84 | return render(request, "home/salescheck.html",context={"object_list":object_list}) 85 | 86 | def addsalescheck(request): 87 | if request.method == 'POST': 88 | record = 销售入账记录() 89 | record.客户 = request.POST['name'] 90 | record.入账日期 = request.POST['created_at'] 91 | record.入账金额 = request.POST['amount'] 92 | record.已到账款项 = request.POST['total'] 93 | record.剩余到账款项 = request.POST['leave'] 94 | 95 | record.save() 96 | return redirect(reverse('home:salescheck')) 97 | else: 98 | return render(request, "home/addsalescheck.html") 99 | -------------------------------------------------------------------------------- /实战案例2/feed.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import json 3 | import subprocess 4 | 5 | from bs4 import BeautifulSoup 6 | import requests 7 | import feedparser 8 | 9 | import time 10 | from datetime import date 11 | 12 | from 新闻 import * 13 | from rag import * 14 | from 生成简报 import * 15 | 16 | def 获取数据(): 17 | # 定义要抓取的RSS源 18 | rss源 = "https://www.cnet.com/rss/news/" 19 | 20 | #region 使用feedparser解析RSS源 21 | feed = feedparser.parse(rss源) 22 | 23 | #region 这段代码纯粹是为了确认我们抓取成功,调试用,可以删除 24 | for entry in feed.entries: 25 | print(entry.title) # 打印标题 26 | print(entry.link) # 打印链接 27 | print(entry.description) # 打印描述 28 | #endregion 29 | 30 | #endregion 31 | 32 | #region 将feed.entries保存到本地json文件 33 | with open('feed.json', 'w', encoding='utf-8') as f: 34 | json.dump(feed.entries, f, ensure_ascii=False, indent=4) 35 | #endregion 36 | return feed.entries 37 | 38 | def 获取元数据(current): 39 | 元数据实例 = 元数据() 40 | 元数据实例.set_标题(current.title) 41 | 元数据实例.set_作者(current.author) 42 | 元数据实例.set_创建日期(current.published_parsed) 43 | 元数据实例.set_url(current.link) 44 | return 元数据实例 45 | 46 | def 根据元数据过滤新闻(current元数据): 47 | 今天 = date.today() 48 | 今天struct_time格式 = time.struct_time(今天.timetuple()) 49 | return current元数据.创建日期 > 今天struct_time格式 50 | 51 | def 抓取新闻内容(url): 52 | response = requests.get(url) 53 | response.encoding = 'utf-8' # 指定编码 54 | soup = BeautifulSoup(response.text, 'html.parser') 55 | 56 | 正文 = soup.find('div', {'class': 'c-pageArticle_content'}) 57 | if 正文 is not None and 正文.text is not None: 58 | return 正文.text 59 | else: 60 | return None 61 | 62 | def 按长度划分文本(输入文本, 文本划分长度): 63 | return [输入文本[i:i+文本划分长度] for i in range(0, len(输入文本), 文本划分长度)] 64 | 65 | def 文本摘要(输入文本): 66 | return 对话模式(构造文本摘要messages(输入文本)) 67 | 68 | def 对长文本进行摘要(输入文本): 69 | if len(输入文本) > 文本划分长度: 70 | 文本list = 按长度划分文本(输入文本, 文本划分长度) 71 | 文本摘要结果 = '' 72 | for 当前文本 in 文本list: 73 | # print('当前文本='+当前文本) 74 | 当前文本摘要结果=文本摘要(当前文本) 75 | # print('当前文本摘要结果='+当前文本摘要结果) 76 | 文本摘要结果 += 当前文本摘要结果 + '\n' 77 | # print('文本摘要结果='+文本摘要结果) 78 | return 文本摘要结果 79 | else: 80 | return 文本摘要(输入文本) 81 | 82 | def 翻译成中文(输入文本): 83 | return 对话模式(构造英译中messages(输入文本)) 84 | 85 | def 对长文本进行翻译(输入文本): 86 | if len(输入文本) > 文本划分长度: 87 | 文本list = 按长度划分文本(输入文本, 文本划分长度) 88 | 文本翻译结果 = '' 89 | for 当前文本 in 文本list: 90 | 当前文本翻译结果=翻译成中文(当前文本) 91 | 文本翻译结果 += 当前文本翻译结果 + '\n' 92 | return 文本翻译结果 93 | else: 94 | return 翻译成中文(输入文本) 95 | 96 | if __name__ == "__main__": 97 | 新闻列表 = 获取数据() 98 | print(f'共获取{len(新闻列表)}条新闻') 99 | 今天的新闻list = [] 100 | 入库条数上限 = 3 # 出于教学和调试目的设置的上限,你可以设置为0,表示不限制入库条数 101 | 当前入库条数 = 0 102 | for current in 新闻列表: 103 | current元数据 = 获取元数据(current) 104 | if 根据元数据过滤新闻(current元数据): 105 | if 入库条数上限 > 0 and 当前入库条数 >= 入库条数上限: 106 | break 107 | 108 | 今天的新闻 = 新闻() 109 | 今天的新闻.set_元数据(current元数据) 110 | print('属于今天的新闻,准备处理') 111 | 今天的新闻.set_新闻内容(抓取新闻内容(今天的新闻.元数据.url)) 112 | if 今天的新闻 is not None and 今天的新闻.新闻内容 is not None: 113 | 今天的新闻.set_摘要(对长文本进行摘要(今天的新闻.新闻内容)) 114 | 今天的新闻.set_标题_中文翻译(翻译成中文(今天的新闻.元数据.标题)) 115 | 今天的新闻.set_新闻内容_中文翻译(对长文本进行翻译(今天的新闻.新闻内容)) 116 | 今天的新闻list.append(今天的新闻) 117 | 当前入库条数 += 1 118 | else: 119 | print('不是今天的新闻,跳过') 120 | 121 | with open('result.json', 'w', encoding='utf-8') as f: 122 | json.dump(今天的新闻list, f, ensure_ascii=False, indent=4, cls=新闻Encoder) 123 | 每日简报文件 = 生成每日简报(今天的新闻list) 124 | 打开每日简报(每日简报文件) 125 | 126 | -------------------------------------------------------------------------------- /实战案例1/改造前/mysite/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for mysite project. 3 | 4 | Generated by 'django-admin startproject' using Django 4.2.13. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.2/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/4.2/ref/settings/ 11 | """ 12 | 13 | import os 14 | from pathlib import Path 15 | 16 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 17 | BASE_DIR = Path(__file__).resolve().parent.parent 18 | 19 | 20 | # Quick-start development settings - unsuitable for production 21 | # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ 22 | 23 | # SECURITY WARNING: keep the secret key used in production secret! 24 | SECRET_KEY = 'django-insecure-i*o!_x6is-f3vx)m7k5-lc!=z873b3coguy4iu045sa*9vu68n' 25 | 26 | # SECURITY WARNING: don't run with debug turned on in production! 27 | DEBUG = True 28 | 29 | ALLOWED_HOSTS = [] 30 | 31 | 32 | # Application definition 33 | 34 | INSTALLED_APPS = [ 35 | 'django.contrib.admin', 36 | 'django.contrib.auth', 37 | 'django.contrib.contenttypes', 38 | 'django.contrib.sessions', 39 | 'django.contrib.messages', 40 | 'django.contrib.staticfiles', 41 | 'home', 42 | ] 43 | 44 | MIDDLEWARE = [ 45 | 'django.middleware.security.SecurityMiddleware', 46 | 'django.contrib.sessions.middleware.SessionMiddleware', 47 | 'django.middleware.common.CommonMiddleware', 48 | 'django.middleware.csrf.CsrfViewMiddleware', 49 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 50 | 'django.contrib.messages.middleware.MessageMiddleware', 51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 52 | ] 53 | 54 | ROOT_URLCONF = 'mysite.urls' 55 | 56 | TEMPLATES = [ 57 | { 58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 59 | 'DIRS': [os.path.join(BASE_DIR, 'templates')], 60 | 'APP_DIRS': True, 61 | 'OPTIONS': { 62 | 'context_processors': [ 63 | 'django.template.context_processors.debug', 64 | 'django.template.context_processors.request', 65 | 'django.contrib.auth.context_processors.auth', 66 | 'django.contrib.messages.context_processors.messages', 67 | ], 68 | }, 69 | }, 70 | ] 71 | 72 | WSGI_APPLICATION = 'mysite.wsgi.application' 73 | 74 | 75 | # Database 76 | # https://docs.djangoproject.com/en/4.2/ref/settings/#databases 77 | 78 | DATABASES = { 79 | 'default': { 80 | 'ENGINE': 'django.db.backends.sqlite3', 81 | 'NAME': BASE_DIR / 'db.sqlite3', 82 | } 83 | } 84 | 85 | 86 | # Password validation 87 | # https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators 88 | 89 | AUTH_PASSWORD_VALIDATORS = [ 90 | { 91 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 92 | }, 93 | { 94 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 95 | }, 96 | { 97 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 98 | }, 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 101 | }, 102 | ] 103 | 104 | 105 | # Internationalization 106 | # https://docs.djangoproject.com/en/4.2/topics/i18n/ 107 | 108 | LANGUAGE_CODE = 'en-us' 109 | 110 | TIME_ZONE = 'UTC' 111 | 112 | USE_I18N = True 113 | 114 | USE_TZ = True 115 | 116 | 117 | # Static files (CSS, JavaScript, Images) 118 | # https://docs.djangoproject.com/en/4.2/howto/static-files/ 119 | 120 | STATIC_URL = 'static/' 121 | 122 | # Default primary key field type 123 | # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field 124 | 125 | DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' 126 | -------------------------------------------------------------------------------- /实战案例1/改造后/mysite/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for mysite project. 3 | 4 | Generated by 'django-admin startproject' using Django 4.2.13. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.2/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/4.2/ref/settings/ 11 | """ 12 | 13 | import os 14 | from pathlib import Path 15 | 16 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 17 | BASE_DIR = Path(__file__).resolve().parent.parent 18 | 19 | 20 | # Quick-start development settings - unsuitable for production 21 | # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ 22 | 23 | # SECURITY WARNING: keep the secret key used in production secret! 24 | SECRET_KEY = 'django-insecure-i*o!_x6is-f3vx)m7k5-lc!=z873b3coguy4iu045sa*9vu68n' 25 | 26 | # SECURITY WARNING: don't run with debug turned on in production! 27 | DEBUG = True 28 | 29 | ALLOWED_HOSTS = [] 30 | 31 | 32 | # Application definition 33 | 34 | INSTALLED_APPS = [ 35 | 'django.contrib.admin', 36 | 'django.contrib.auth', 37 | 'django.contrib.contenttypes', 38 | 'django.contrib.sessions', 39 | 'django.contrib.messages', 40 | 'django.contrib.staticfiles', 41 | 'home', 42 | ] 43 | 44 | MIDDLEWARE = [ 45 | 'django.middleware.security.SecurityMiddleware', 46 | 'django.contrib.sessions.middleware.SessionMiddleware', 47 | 'django.middleware.common.CommonMiddleware', 48 | 'django.middleware.csrf.CsrfViewMiddleware', 49 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 50 | 'django.contrib.messages.middleware.MessageMiddleware', 51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 52 | ] 53 | 54 | ROOT_URLCONF = 'mysite.urls' 55 | 56 | TEMPLATES = [ 57 | { 58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 59 | 'DIRS': [os.path.join(BASE_DIR, 'templates')], 60 | 'APP_DIRS': True, 61 | 'OPTIONS': { 62 | 'context_processors': [ 63 | 'django.template.context_processors.debug', 64 | 'django.template.context_processors.request', 65 | 'django.contrib.auth.context_processors.auth', 66 | 'django.contrib.messages.context_processors.messages', 67 | ], 68 | }, 69 | }, 70 | ] 71 | 72 | WSGI_APPLICATION = 'mysite.wsgi.application' 73 | 74 | 75 | # Database 76 | # https://docs.djangoproject.com/en/4.2/ref/settings/#databases 77 | 78 | DATABASES = { 79 | 'default': { 80 | 'ENGINE': 'django.db.backends.sqlite3', 81 | 'NAME': BASE_DIR / 'db.sqlite3', 82 | } 83 | } 84 | 85 | 86 | # Password validation 87 | # https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators 88 | 89 | AUTH_PASSWORD_VALIDATORS = [ 90 | { 91 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 92 | }, 93 | { 94 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 95 | }, 96 | { 97 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 98 | }, 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 101 | }, 102 | ] 103 | 104 | 105 | # Internationalization 106 | # https://docs.djangoproject.com/en/4.2/topics/i18n/ 107 | 108 | LANGUAGE_CODE = 'en-us' 109 | 110 | TIME_ZONE = 'UTC' 111 | 112 | USE_I18N = True 113 | 114 | USE_TZ = True 115 | 116 | 117 | # Static files (CSS, JavaScript, Images) 118 | # https://docs.djangoproject.com/en/4.2/howto/static-files/ 119 | 120 | STATIC_URL = 'static/' 121 | 122 | # Default primary key field type 123 | # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field 124 | 125 | DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' 126 | --------------------------------------------------------------------------------