当前位置: 首页 > news >正文

移动互联网开发课件云速seo百度点击

移动互联网开发课件,云速seo百度点击,个旧建设局信息公开门户网站,智通人才招聘网最新招聘文章目录 一.先利用langchain官方文档的AI功能问问二.langchain async api三.串行,异步速度比较 一.先利用langchain官方文档的AI功能问问 然后看他给的 Verified Sources 这个页面里面虽然有些函数是异步函数,但是并非专门讲解异步的 二.langchain asy…

文章目录

  • 一.先利用langchain官方文档的AI功能问问
  • 二.langchain async api
  • 三.串行,异步速度比较

一.先利用langchain官方文档的AI功能问问

在这里插入图片描述

  • 然后看他给的 Verified Sources
    在这里插入图片描述
  • 这个页面里面虽然有些函数是异步函数,但是并非专门讲解异步的

二.langchain async api

还不如直接谷歌搜😂 一下搜到, 上面那个AI文档问答没给出这个链接

在这里插入图片描述

  • 官方示例

    import asyncio
    import timefrom langchain.llms import OpenAI
    from langchain.prompts import PromptTemplate
    from langchain.chains import LLMChaindef generate_serially():llm = OpenAI(temperature=0.9)prompt = PromptTemplate(input_variables=["product"],template="What is a good name for a company that makes {product}?",)chain = LLMChain(llm=llm, prompt=prompt)for _ in range(5):resp = chain.run(product="toothpaste")print(resp)async def async_generate(chain):resp = await chain.arun(product="toothpaste")print(resp)async def generate_concurrently():llm = OpenAI(temperature=0.9)prompt = PromptTemplate(input_variables=["product"],template="What is a good name for a company that makes {product}?",)chain = LLMChain(llm=llm, prompt=prompt)tasks = [async_generate(chain) for _ in range(5)]await asyncio.gather(*tasks)s = time.perf_counter()
    # If running this outside of Jupyter, use asyncio.run(generate_concurrently())
    await generate_concurrently()
    elapsed = time.perf_counter() - s
    print("\033[1m" + f"Concurrent executed in {elapsed:0.2f} seconds." + "\033[0m")s = time.perf_counter()
    generate_serially()
    elapsed = time.perf_counter() - s
    print("\033[1m" + f"Serial executed in {elapsed:0.2f} seconds." + "\033[0m")
    
  • 不过官方代码报错了
    在这里插入图片描述

  • 我让copilot修改一下,能跑了

    import time
    import asyncio
    from langchain.llms import OpenAI
    from langchain.prompts import PromptTemplate
    from langchain.chains import LLMChaindef generate_serially():llm = OpenAI(temperature=0.9)prompt = PromptTemplate(input_variables=["product"],template="What is a good name for a company that makes {product}?",)chain = LLMChain(llm=llm, prompt=prompt)for _ in range(5):resp = chain.run(product="toothpaste")print(resp)async def async_generate(chain):resp = await chain.arun(product="toothpaste")print(resp)async def generate_concurrently():llm = OpenAI(temperature=0.9)prompt = PromptTemplate(input_variables=["product"],template="What is a good name for a company that makes {product}?",)chain = LLMChain(llm=llm, prompt=prompt)tasks = [async_generate(chain) for _ in range(5)]await asyncio.gather(*tasks)async def main():s = time.perf_counter()await generate_concurrently()elapsed = time.perf_counter() - sprint("\033[1m" + f"Concurrent executed in {elapsed:0.2f} seconds." + "\033[0m")s = time.perf_counter()generate_serially()elapsed = time.perf_counter() - sprint("\033[1m" + f"Serial executed in {elapsed:0.2f} seconds." + "\033[0m")asyncio.run(main())

    在这里插入图片描述

  • 这还有一篇官方blog
    在这里插入图片描述
    在这里插入图片描述

三.串行,异步速度比较

  • 先学习一下掘金上看到的一篇:https://juejin.cn/post/7231907374688436284
  • 为了更方便的看到异步效果,我在原博主的基础上,print里面加了一个提示
    在这里插入图片描述
    在这里插入图片描述
# 引入time和asyncio模块
import time
import asyncio
# 引入OpenAI类
from langchain.llms import OpenAI# 定义异步函数async_generate,该函数接收一个llm参数和一个name参数
async def async_generate(llm, name):# 调用OpenAI类的agenerate方法,传入字符串列表["Hello, how are you?"]并等待响应resp = await llm.agenerate(["Hello, how are you?"])# 打印响应结果的生成文本和函数名print(f"{name}: {resp.generations[0][0].text}")# 定义异步函数generate_concurrently
async def generate_concurrently():# 创建OpenAI实例,并设置temperature参数为0.9llm = OpenAI(temperature=0.9)# 创建包含10个async_generate任务的列表tasks = [async_generate(llm, f"Function {i}") for i in range(10)]# 并发执行任务await asyncio.gather(*tasks)# 主函数
# 如果在Jupyter Notebook环境运行该代码,则无需手动调用await generate_concurrently(),直接在下方执行单元格即可执行该函数
# 如果在命令行或其他环境下运行该代码,则需要手动调用asyncio.run(generate_concurrently())来执行该函数
asyncio.run(generate_concurrently())

免费用户一分钟只能3次,实在是有点难蚌

在这里插入图片描述

  • 整合一下博主的代码,对两个速度进行比较,但是这个调用限制真的很搞人啊啊啊

    import time
    import asyncio
    from langchain.llms import OpenAIasync def async_generate(llm, name):resp = await llm.agenerate(["Hello, how are you?"])# print(f"{name}: {resp.generations[0][0].text}")async def generate_concurrently():llm = OpenAI(temperature=0.9)tasks = [async_generate(llm, f"Function {i}") for i in range(3)]await asyncio.gather(*tasks)def generate_serially():llm = OpenAI(temperature=0.9)for _ in range(3):resp = llm.generate(["Hello, how are you?"])# print(resp.generations[0][0].text)async def main():s = time.perf_counter()await generate_concurrently()elapsed = time.perf_counter() - sprint("\033[1m" + f"Concurrent executed in {elapsed:0.2f} seconds." + "\033[0m")s = time.perf_counter()generate_serially()elapsed = time.perf_counter() - sprint("\033[1m" + f"Serial executed in {elapsed:0.2f} seconds." + "\033[0m")asyncio.run(main())
    

    在这里插入图片描述
    在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 再看一篇blog
    • 作者将代码开源在这里了:https://github.com/gabrielcassimiro17/async-langchain
    • 测试一下它的async_chain.py文件
      在这里插入图片描述
  • 读取csv的时候路径一直报错,还好不久前总结了一篇blog:Python中如何获取各种目录路径
    • 直接获取当前脚本路径了

      import os
      import pandas as pd# Get the directory where the script is located
      script_directory = os.path.dirname(os.path.abspath(__file__))# Construct the path to the CSV file
      csv_path = os.path.join(script_directory, 'wine_subset.csv')# Read the CSV file
      df = pd.read_csv(csv_path)
      
      • sequential_run.py 就不跑了… 一天200次调用都快没了
  • 主要是看看两者区别
    在这里插入图片描述
http://www.hrbkazy.com/news/45702.html

相关文章:

  • 龙游县建设局网站百度指数查询app
  • 沈阳做网站比较好的公司网络广告是什么
  • 建立网站最先进的互联网技术有哪些百度投诉中心
  • wordpress 热门用户汕头seo按天付费
  • 天津公司网站如何制作湖南百度seo排名点击软件
  • 高端大气的科技网站成免费crm特色
  • 沈阳seo排名优化推广深圳seo公司
  • 深圳外贸建站网络推广公司如何进行电子商务网站推广
  • 鲜花网站的数据库建设今日新闻事件
  • 网站建设的功能需求青岛网站快速排名提升
  • 烟台seo网站推广费用网络营销的未来发展趋势
  • 注册网站做网销电子商务营销策划方案
  • 建设信用卡银行积分兑换商城网站推广公司简介
  • 无锡高端网站建设机构软文网站发布平台
  • 网站建设费用如何入账谷歌seo推广
  • 电子商务网站软件建设的搜外滴滴友链
  • 网站前台如何刷新seo的基本工作内容
  • wordpress用户注册表深圳网站优化软件
  • 生物制药公司网站建设品牌推广策略
  • 网站开发是否属于技术合同推广软文是什么
  • 免费做简历的软件网站免费开发网站
  • 尤溪网站开发在线优化seo
  • 上传网站页面打不开怎么办百度网盘客服
  • 凡科网多页网站怎样做郴州网站定制
  • 广州市番禺区人民政府门户网站外贸网络营销平台
  • 简单的做海报的网站艾滋病多久能查出来
  • 微信小程序网站建设哪家好网站流量分析报告
  • 淮北哪有做淘宝网站seo外包软件
  • 建e网室内设计网现代简约淘宝优化
  • 不用淘宝客api如何做网站谷歌搜索引擎为什么国内用不了