Python AI 速成课:快速打造高效AI

发布于:2024-05-08 ⋅ 阅读:(30) ⋅ 点赞:(0)

这个文章主要是对python AI 小白的 。实用性很强,不用太多复杂步骤。

第一步:先到Try NVIDIA NIM APIs这个网站

然后使用邮箱注册,很简单和快捷。

第二步:点击微软的phi-3模型,这个API是免费的

第三步: 获取完该API的密钥 ,点击复制

 第四步:直接复制就行,密钥在生成的时候就自动添在代码中了。

 第五步:复制到pycharm中运行效果

第六步:有兴趣的可以改一下代码,做个图形界面版的。如下:

 注意改成自己的API密钥!

import tkinter as tk
from openai import OpenAI

def get_response():
    demo = input_box.get()
    completion = client.chat.completions.create(
        model="microsoft/phi-3-mini-128k-instruct",
        messages=[{"role": "user", "content": demo}],
        temperature=0.2,
        top_p=0.7,
        max_tokens=1024,
        stream=True
    )
    response = ""
    for chunk in completion:
        if chunk.choices[0].delta.content is not None:
            response += chunk.choices[0].delta.content
    output_box.delete(1.0, tk.END)
    output_box.insert(tk.END, response)

client = OpenAI(
    base_url="https://integrate.api.nvidia.com/v1",
    api_key="API接口密钥"                                #输入自己的API密钥
)

root = tk.Tk()
root.title("微软的phi-3 AI大模型")

input_label = tk.Label(root, text="请输入你的内容:")
input_label.pack()

input_box = tk.Entry(root, width=50)
input_box.pack()

submit_button = tk.Button(root, text="提交", command=get_response)
submit_button.pack()

output_label = tk.Label(root, text="输出结果:")
output_label.pack()

output_box = tk.Text(root, wrap=tk.WORD, width=50, height=10)
output_box.pack()

root.mainloop()

下面是效果图:

如果喜欢的话可以给我赞!谢谢支持!