首页 > 其他分享 >openai包基础用法

openai包基础用法

时间:2024-04-17 22:13:44浏览次数:14  
标签:stream messages 基础 用法 content openai async model response

Note

包含同步&异步 完成&流式
闲言少叙, 看剑

Requirements

pip install openai -U

Code

import openai
import asyncio


def pp(obj: str):
    print(obj.center(50, "*"))


# sync
def _sync():
    ## w/o stream
    pp("Sync w/o stream")
    response = client.chat.completions.create(
        model=model, messages=messages, temperature=0
    )
    print(response.choices[0].message.content)

    ## w/ stream
    pp("Sync w/ stream")
    response = client.chat.completions.create(
        model=model, messages=messages, temperature=0, stream=True
    )
    for chunk in response:
        content = chunk.choices[0].delta.content
        if content:
            print(content, end="", flush=True)
        if chunk.choices[0].finish_reason == "stop":
            print()


# async
async def _async():
    # w/o stream
    pp("Async w/o stream")
    async_response = await aclient.chat.completions.create(
        model=model, messages=messages, temperature=0
    )
    print(async_response.choices[0].message.content)

    ## w/ stream
    pp("Async w/ stream")

    async_response_with_stream = await aclient.chat.completions.create(
        model=model, messages=messages, temperature=0, stream=True
    )
    async for chunk in async_response_with_stream:
        content = chunk.choices[0].delta.content
        if content:
            print(content, end="", flush=True)
        if chunk.choices[0].finish_reason == "stop":
            print()


async def main():
    _sync()
    await _async()


if __name__ == "__main__":
    client = openai.OpenAI()
    aclient = openai.AsyncOpenAI()

    model = "Qwen1.5-32B-Chat"
    messages = [{"role": "user", "content": "Who are you"}]
    asyncio.run(main())

标签:stream,messages,基础,用法,content,openai,async,model,response
From: https://www.cnblogs.com/shy36/p/18141876

相关文章

  • 前端【小程序】04-小程序基础篇【分包加载】
    一、分包加载官方文档:https://developers.weixin.qq.com/miniprogram/dev/framework/subpackages.html​分包加载是优化小程序加载速度的一种手段。1.1为什么?​微信平台对小程序单个包的代码体积限制为2M,超过2M的情况下可以采用分包来解决即使小程序代码体积没......
  • 前端【小程序】04-小程序基础篇【生命周期】
    生命周期生命周期是一些名称固定自动执行的函数。 页面生命周期​onLoad 在页面加载完成时执行,只会执行1次,常用于获取地址参数和网络请求onShow 在页面处于可见状态时执行,常用于动态更新数据或状态onReady 在页面初次渲染完成时执行,只会执行1次,常用于节点操作或......
  • mysql中replace into用法
    前言replaceinto跟insertinto功能类似,不同点在于:replaceinto首先尝试插入数据到表中如果发现表中已经有相同的数据(根据主键或者唯一索引判断)则先删除原来的数据,然后插入新的。否则,直接插入新数据。注意:插入数据的表必须有主键或者是唯一索引!否则的话,replaceinto会......
  • ES6中数组的高级用法
    1.箭头函数和数组方法的结合:使用箭头函数结合数组方法可以简化代码:constnumbers=[1,2,3,4,5];//使用箭头函数的map方法constdoubled=numbers.map((num)=>num*2);console.log(doubled);//输出:[2,4,6,8,10]2.解构赋值和数组方法的结合:constpoi......
  • Python中operator 模块的用法
    operator模块提供了一套与Python的内置运算符对应的高效率函数。1.函数的种类函数包含的种类有:对象的比较运算、逻辑运算、数学运算和序列运算2.比较运算运算函数语法小于lt(a,b)a<b小于等于le(a,b)a<=b大于gt(a,b)a>b大于等于ge(a,b)......
  • Python中pathlib 模块的用法
    pathlib模块提供了表示文件系统路径的类,可适用于不同的操作系统。使用pathlib模块,相比于os模块可以写出更简洁,易读的代码。pathlib模块中的Path类继承自PurePath,对PurePath中的部分方法进行了重载,相比于os.path有更高的抽象级别。本文将带你学习如何使用pathlib......
  • Python中itertools 模块的用法
    在Python中,迭代器是一种非常好用的数据结构,其最大的优势就是延迟生成,按需使用,从而大大提高程序的运行效率。而itertools作为Python的内置模块,就为我们提供了一套非常有用的用于操作可迭代对象的函数。常用功能1.count功能详解count(start=0,step=1)函数有两个参数,其中......
  • Markdown基础用法
    目录Markdown基础用法标题一级标题二级标题字体引用图片超链接表格分隔线代码:Markdown基础用法本文是关于Markdown的基础用法,不涉及高级用法,只供自己学习总结使用标题#一级标题##二级标题(如有需求,可以有更多)实际效果:一级标题二级标题字体*你好*斜体**你好**......
  • 前端【小程序】03-小程序基础篇【组件】【导航】【图片】【轮播图】【表单】【区域滚
    navigator文档:https://developers.weixin.qq.com/miniprogram/dev/component/navigator.htmlurl:页面路径•支持相对和绝对路径•路径为空时会报错hover-class:点击态的样式,默认按下时会有一个样式•none禁用点击效果open-type:跳转方......
  • 持续性学习-Day14(前端基础HTML5)
    参考教学视频:秦疆 HTML(HyperTexctMarkupLanguage)超文本标记语言W3C:WorldWideWebConsortium万维网联盟W3C标准包括:结构化标准语言(HTML、XML)表现标准语言(CSS)行为标准(DOM、EXMAScript)1.基本标签<!--标题标签--><!--h1-h5--><!--段落标签--><p></p><!--......