1.配置embedding模型
在同事hysun的text2sql rag开源项目中,提供了一些非常实用的配置方法,方便大家使用,本文就以此开源项目为例。
我这里embedding模型暂时选择的是 siliconflow 平台提供的 baai/bge-large-zh-v1.5 这个模型:
--使用 siliconflow embedding:
begin
custom_select_ai.create_embedding_conf(
p_conf_id => 'embedding',
p_provider => 'openai',
p_model => 'baai/bge-large-zh-v1.5',
p_endpoint => 'https://api.siliconflow.cn/v1/embeddings',
p_credential => 'alfred_silicon_flow'
);
end;
/起初我直接把api key写到了p_credential中,发现不行,但是换成自定义的alfred_silicon_flow后,通过dbms_vector.create_credential导入时,起初以为直接传json格式的参数,结果是不允许的。
另外,注意这里p_conf_id的名字,以后要用到,开始我没意识到,咨询同事后才知晓这个对应关系。后来同事为了大家更好理解,还修改了帮助文档,明确做了提示。
2.特殊语法传参json格式
翻阅官方文档,找到这种特殊的语法,专门用于传入json格式。
--特殊的语法,传入json格式方法
declare
jo json_object_t;
begin
jo := json_object_t();
jo.put('access_token', 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
dbms_vector.create_credential(
credential_name => 'alfred_silicon_flow',
params => json(jo.to_string));
end;
/api key已脱敏,按照你的key实际替换即可。
配置好之后,可以通过这张表查询到:
select * from custom_select_ai_embedding_conf;
3.测试embedding有效
使用提供的测试用例来测试embedding有效性:
--embedding接口 - 文本转向量
select custom_select_ai.embedding(
p_text => '将文本转成向量',
p_embedding_conf => 'embedding'
);注意这个p_embedding_conf需要和之前创建时的p_conf_id对应,后面使用都有这个对应关系,需要特别注意下。
测试返回报错:
ora-06502: pl/sql:值或转换错误 : 字符串缓冲区太小
ora-06512: 在 line 1https://docs.oracle.com/error-help/db/ora-06502/06502. 00000 - "pl/sql: value or conversion error%s"
*cause: an arithmetic, numeric, string, conversion, or constraint error
occurred. for example, this error occurs if you attempt to
assign the value null to a variable declared not null, or if you
attempt to assign an integer greater than 99 to a variable
declared number(2).
*action: to resolve the issue, change the data, the way the data is
manipulated, or the data variable declaration.
*params: 1) error_info
occurred.
4.修改max_string_size
上面的报错很明显,文档中也有提到需要设置max_string_size为extended才可以。
修改的参考步骤(生产环境请慎重评估可行性):
--1.查看参数当前值 show parameter max_string_size; --2.设置max_string_size=extended alter system set max_string_size=extended scope=spfile; --3.关闭数据库 shutdown immediate; --4.启动upgrade模式,执行脚本 startup upgrade; @$oracle_home/rdbms/admin/utl32k.sql --5.重启数据库 shutdown immediate; startup; --6.查询max_string_size参数已修改 show parameter max_string_size;
再次运行embedding测试用例,成功返回结果。
5.配置为deepseek的llm
主要用到两个custom_select_ai.create_provider、custom_select_ai.create_profile。
----- create service provider,deepseek
begin
custom_select_ai.create_provider(
p_provider => 'openai',
p_endpoint => 'https://api.deepseek.com/chat/completions',
p_auth => 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
);
end;
/
----- create profile
begin
custom_select_ai.create_profile(
p_profile_name =>'hke_demo',
p_description => 'selectai demo for hke',
p_attributes => '{
"provider": "openai",
"model" : "deepseek-chat",
"object_list": [{"owner": "tpch", "name": "hke_prod_defect"},
{"owner": "tpch", "name": "hke_prod_out_yield_qty"}
]
}'
);
end;
/创建好之后可以查询到:
--创建好的provider和profile可以查询表: select * from tpch.custom_select_ai_providers; select * from tpch.custom_select_ai_profiles;
配置错误或不再需要,可以这样删除掉:
--删除不再需要的provider和profile: begin custom_select_ai.drop_provider( p_provider => 'openai' ); end; / begin custom_select_ai.drop_profile( p_profile_name =>'hke_demo' ); end; /
6.测试chat和showsql有效
按照开源项目文档中的说明,测试chat和showsql功能:
--chat接口 - 直接与 llm 聊天
select custom_select_ai.chat(
p_profile_name => 'hke_demo',
p_user_text => '你是谁?',
p_system_text => '你是一个积极的、充满正能量的人工智能助手。'
);注意:这里chat配置好llm应该就可以正常返回,如果报错ora-29273,如下图所示:
- 4-sys.utl_http.png
就需要放开对应数据库用户对具体或所有外部的访问:
begin
dbms_network_acl_admin.append_host_ace(
host => '*', -- 或者指定具体的域名,如 'api.example.com'
ace => xs$ace_type(privilege_list => xs$name_list('connect'),
principal_name => 'tpch',
principal_type => xs_acl.ptype_db));
end;
/继续测试showsql功能:
--showsql接口 - 自然语言生成sql
select custom_select_ai.showsql(
p_profile_name => 'hke_demo',
p_embedding_conf => 'embedding',
p_user_text => '查询符合条件的各yield小等级占比(即yield_qty之和/out_qty之和),条件为:公司名称为company1,工厂名称为factoryname1,产品名称为product1。占比用百分比表示并排序,用中文别名返回。'
);showsql需要按demo要求导入成功表数据并向量化才ok。这里的p_embedding_conf要注意和之前配置的embedding名字一样。
到此这篇关于oracle ai应用的llm模型典型配置的文章就介绍到这了,更多相关oracle llm模型内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论