一、基于文心一言的【金融产品营销生成大师】
你是金融产品营销大师啦!!!
真是超简单 项目地址: aistudio.baidu.com/aistudio/pr...
二、环境需求
- 安装aistudio包
- 安装gradio
- 写gradio应用并部署
python
%capture
!pip install https://studio-package.bj.bcebos.com/aistudio-0.0.2-py3-none-any.whl
三、文心一眼API介绍
python
import os
import aistudio
os.environ["AISTUDIO_LOG"] = "debug"
1.单轮对话测试
python
# 创建单轮对话
chat_completion = aistudio.chat.create(
messages=[
{
"role": "user",
"content": "⽤可爱的语⽓介绍⼀下你⾃⼰"
}
]
)
print(chat_completion. result)
arduino
{'messages': [{'role': 'user', 'content': '⽤可爱的语⽓介绍⼀下你⾃⼰'}]}
你好呀!我是文心一言,英文名是ERNIE Bot。我是一款可爱的AI语言模型,能为大家提供准确、实用的知识信息。我会尽我所能,帮助大家解决问题,如果你有任何需要,请随时告诉我哦!
2.embedding测试
python
import aistudio
embeddings = aistudio.embed.embedding_v1(
input=["推荐⼀些美⻝", "给我讲个故事"] ,
)
print(embeddings)
arduino
{'id': 'as-w114gu7frh', 'object': 'embedding_list', 'created': 1691394285, 'data': [{'object': 'embedding', 'embedding': [-0.09448352456092834, 0.03004167228937149, 0.07719703763723373, 0.02995435707271099, 0.06377328187227249, -0.17602227628231049, 0.04201335459947586, -0.03499295562505722, 0.012784539721906185, -0.009622186422348022, 0.011525488458573818, -0.03760962188243866, 0.03805296868085861, -0.14806322753429413, -0.04416503757238388, 0.09223702549934387, 4.189325863990234e-06, 0.015531143173575401, 0.018460897728800774, 0.11139625310897827, 0.024054860696196556, -0.026803703978657722, 0.0864797830581665, 0.020414702594280243, 0.12078870087862015, 0.051731254905462265, -0.06892452389001846, -0.0422443188726902, -0.008922722190618515, 0.11700121313333511, -0.03679785132408142, -0.09260254353284836, 0.08038699626922607, -0.03166491538286209, -0.05953741446137428, 0.0743323415517807, 0.03642565384507179, 0.0005728728719986975, 0.11255331337451935, -0.09387871623039246, -0.06274320
3.gradio脚本
python
import os
os.system('pip install -U pip')
# 1.安装aistudio包
os.system('pip install https://studio-package.bj.bcebos.com/aistudio-0.0.2-py3-none-any.whl')
import aistudio
os.environ["AISTUDIO_LOG"] = "debug"
import gradio as gr
def clear_session():
return '', None
# 2.生成对话内容
def predict(message,
history=None):
if history == None:
history = []
print(message)
# 3.创建单轮对话
chat_completion = aistudio.chat.create(
messages=[
{
"role": "user",
"content": '你是一名专业的推销金融产品的文案生成员,工作是为金融产品写一些专业的,吸引人的营销文案,营销文案具有流畅性和结构性,接下来用户会提供金融产品名字,金融产品文案风格。以下是输入:'+ message
}
]
)
print(chat_completion.result)
history.append((message, chat_completion.result))
return '', history, history
if __name__ == "__main__":
block = gr.Blocks()
# 4.【金融产品营销生成大师】介绍
with block as demo:
gr.Markdown("""<h1><center>基于文心一言的【金融产品营销生成大师】</center></h1>
<center>
<h3>基于文心一言API</h3>
<h3>输入:产品名称:"复工贷",文案风格:激昂的,带有鼓励意味的100字 </h3>
<h3>输出:复工贷,助力您的企业重启生机!我们为您提供迅速、灵活的融资支持,让您在疫情后的新机遇中领先一步。激昂的复工贷,带着鼓励的意味,为您注入信心与力量,共同书写更加辉煌的未来! </h3>
</center>
""")
with gr.Row():
with gr.Column():
chatbot = gr.Chatbot(label='ChatLLM')
message = gr.Textbox(label='输入' value='产品名称:"复工贷",文案风格:激昂的,带有鼓励意味的100字 ')
state = gr.State()
with gr.Row():
clear_history = gr.Button("🧹 清除历史对话")
send = gr.Button("🚀 发送")
# 5.相应提交
send.click(predict,
inputs=[
message, state
],
outputs=[message, chatbot, state])
# 6.响应清空
clear_history.click(fn=clear_session,
inputs=[],
outputs=[chatbot, state],
queue=False)
# 7.响应回车提交
message.submit(predict,
inputs=[
message, state
],
outputs=[message, chatbot, state])
gr.Markdown("""- 可放开使用,终于不怕金币耗光了!!! """)
# 8.启动
demo.queue().launch(server_name='0.0.0.0', share=False)
4.部署
看图说话吧!