本地大模型5:在pycharm用python调用ollama本地大模型

建议:如果想获取更多关于ollama本地大模型的信息,可以查看博主的其他相关文章,也可以前往博客园学习:Ollama完整教程:本地LLM管理、WebUI对话、Python/Java客户端API应用 - 老牛啊 - 博客园 (cnblogs.com)

第一步,安装 Python 依赖包:

在pycharm终端输入:

pip install ollama

下载不了可以用镜像下载:

pip install ollama -i https://pypi.tuna.tsinghua.edu.cn/simple

第二步,调用 Ollama 接口,以调用【llama3.1:8b】为例(代码种有两处需要修改大模型名称)

注意:需要启动ollama的对应大模型,并且关闭梯子

启动大模型llama3.1:8b:Win+R调出运行框,输入cmd,在cmd中输入【ollama run llama3.1:8b】并启动

import ollama


# 流式输出
def api_generate(text: str):
    print(f'提问:{text}')

    stream = ollama.generate(
        stream=True,
        model='llama3.1:8b', # 修改大模型名称1
        prompt=text,
    )

    print('-----------------------------------------')
    for chunk in stream:
        if not chunk['done']:
            print(chunk['response'], end='', flush=True)
        else:
            print('\n')
            print('-----------------------------------------')
            print(f'总耗时:{chunk['total_duration']}')
            print('-----------------------------------------')


if __name__ == '__main__':
    # 流式输出
    api_generate(text='天空为什么是蓝色的?')

    # 非流式输出
    content = ollama.generate(model='llama3.1:8b', prompt='天空为什么是蓝色的?') # 修改大模型名称2
    print(content)

运行结果截图(回答会不一样):

代码2.0(可以在运行后输入问题获得回答,输入退出结束运行):

import ollama


# 流式输出
def api_generate(text: str):
    print(f'提问:{text}')

    stream = ollama.generate(
        stream=True,
        model='llama3.1:8b',  # 修改大模型名称1
        prompt=text,
    )

    print('-----------------------------------------')
    for chunk in stream:
        if not chunk['done']:
            print(chunk['response'], end='', flush=True)
        else:
            print('\n')
            print('-----------------------------------------')
            print(f'总耗时:{chunk["total_duration"]}')
            print('-----------------------------------------')


if __name__ == '__main__':
    while True:
        # 从用户获取输入
        question = input("请输入你的问题(输入 '退出' 以结束程序):")

        # 检查用户是否想退出
        if question.lower() in ['退出', 'exit', 'quit']:
            print("程序已退出。")
            break

        # 流式输出
        api_generate(text=question)

        # 非流式输出
        content = ollama.generate(model='llama3.1:8b', prompt=question)  # 修改大模型名称2
        print(content)

当你安装了Python 3.10.4并在PyCharm中设置后,出现"unsupported Python 3.1"的报错是因为你所使用的PyCharm版本较低。 这个问题可以通过两种解决方案来解决。一种是降低Python环境的版本,取消使用3.10,改用3.7或者3.8等较低版本。另一种是下载较高版本的PyCharm。 如果你必须使用Python 3.10,那么你可以选择卸载PyCharm 2019并安装一个较高版本的PyCharm。 这样,当你安装并打开较高版本的PyCharm后,它将能够识别Python 3.10环境,从而解决了"unsupported Python 3.1"的报错问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [pycharm unsupported Python 3.1](https://blog.csdn.net/chanelwtt/article/details/123848162)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [pycharm unsupported Python 3.1报错:pycharm无法识别python 3.10](https://blog.csdn.net/weixin_44346972/article/details/129658688)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值