
当然,后续如果官方API key开放充值了,也可以参考这篇文章的方法。
https://cloud.siliconflow.cn/account/ak


setx SILICONFLOW_API_KEY xxx
export SILICONFLOW_API_KEY xxx
pip install --upgrade openai
from openai import OpenAIimport osapi_key = os.getenv('SILICONFLOW_API_KEY')client = OpenAI(api_key=api_key, base_url="https://api.siliconflow.cn/v1")response = client.chat.completions.create(model='deepseek-ai/DeepSeek-V3',messages=[{'role': 'user','content': "2025年,对创业者来说,有哪些机遇?"}],stream=True)for chunk in response:print(chunk.choices[0].delta.content, end='')

https://chat.deepseek.com/
借助gradio实现一个页面,有两个输入框
一个框输入表结构,另一个框输入SQL语句
然后替换以下代码中的content
一个输出框输出优化结果
from openai import OpenAIimport osapi_key = os.getenv('SILICONFLOW_API_KEY')client = OpenAI(api_key=api_key, base_url="https://api.siliconflow.cn/v1")response = client.chat.completions.create(model='deepseek-ai/DeepSeek-V3',messages=[{'role': 'user','content': "2025年,对创业者来说,有哪些机遇?"}],stream=True)for chunk in response:print(chunk.choices[0].delta.content, end='')
小编的会话截图如下:

生成的代码是:
from openai import OpenAIimport osimport gradio as grdef optimize_sql(schema, sql):# 组合提示词prompt = f"""请优化以下SQL语句:【表结构】{schema}【原始SQL】{sql}【优化建议】"""# 创建OpenAI客户端client = OpenAI(api_key=os.getenv('SILICONFLOW_API_KEY'),base_url="https://api.siliconflow.cn/v1")# 流式请求response = client.chat.completions.create(model='deepseek-ai/DeepSeek-V3',messages=[{'role': 'user', 'content': prompt}],stream=True)# 逐步收集并返回结果full_response = ""for chunk in response:content = chunk.choices[0].delta.content or ""full_response += contentyield full_responsewith gr.Blocks(title="SQL优化助手") as demo:gr.Markdown("## SQL优化助手")gr.Markdown("请输入表结构和需要优化的SQL语句")with gr.Row():with gr.Column():schema_input = gr.Textbox(label="表结构",placeholder="请输入表结构(例如:users(id INT, name VARCHAR(50), age INT))",lines=7)sql_input = gr.Textbox(label="SQL语句",placeholder="请输入需要优化的SQL语句",lines=7)output = gr.Textbox(label="优化结果",interactive=False,lines=12)gr.Examples(examples=[["employees(id INT, name VARCHAR(100), department_id INT, salary DECIMAL(10,2))","SELECT * FROM employees WHERE salary > 5000"],["orders(order_id INT, customer_id INT, order_date DATE, total_amount DECIMAL)","SELECT customer_id, COUNT(*) FROM orders GROUP BY customer_id HAVING total_amount > 1000"]],inputs=[schema_input, sql_input])submit_btn = gr.Button("开始优化", variant="primary")submit_btn.click(fn=optimize_sql,inputs=[schema_input, sql_input],outputs=output)if __name__ == "__main__":demo.queue().launch()

http://127.0.0.1:7860/

CREATE TABLE `t1` (`id` int(11) NOT NULL AUTO_INCREMENT,`a` varchar(20) DEFAULT NULL,`b` int(20) DEFAULT NULL,`c` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,PRIMARY KEY (`id`),KEY `idx_a` (`a`) USING BTREE) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
select * from t1 where a=1000;

https://www.maliustudy.com/detail/column/307
内容介绍
第 01 节:当官方DeepSeek不能用时,有哪些平替方案
第 02 节:私有化部署DeepSeek
第 03 节:DeepSeek API使用
第 04 节:借助DeepSeek API实现SQL优化平台
第 05 节:配置DeepSeek知识库
第 06 节:让DeepSeek成为你的编程助手
第 07 节:通过DeepSeek实现一个智能客服
文章转载自MySQL数据库联盟,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




