微信扫码
与创始人交个朋友
我要投稿
本文介绍llamaindex的工作流的使用。这是一个简单的例子,可以在这个例子的基础上进行修改和创建自己的工作流。
本文的测试使用本地大模型llama3.2(通过ollama可以很容易替换成其他本地模型)和ollama框架,不需要访问openai的接口,这样就可以私有化部署运行。
(1)通过Ollama来指定本地大模型,我这里指定的是llama3.2;
(2)需要安装asyncio,异步调用框架;
(3)代码的第一个工作流节点首先会产生一个joke,后面的节点会评价这个joke,然后就结束了。
from llama_index.core.workflow import (
Event,
StartEvent,
StopEvent,
Workflow,
step,
)
from llama_index.core import Settings
from llama_index.llms.ollama import Ollama
import asyncio
# 指定本地大模型
Settings.llm = Ollama(model="llama3.2", request_timeout=360)
# 自定义事件
class JokeEvent(Event):
joke: str
# 指定工作流
class JokeFlow(Workflow):
#llm = OpenAI()
llm = Settings.llm
@step
async def generate_joke(self, ev: StartEvent) -> JokeEvent:
topic = ev.topic
prompt = f"Write your best joke about {topic}."
response = await self.llm.acomplete(prompt)
return JokeEvent(joke=str(response))
@step
async def critique_joke(self, ev: JokeEvent) -> StopEvent:
joke = ev.joke
print(f"joke:[{joke}]")
prompt = f"Give a thorough analysis and critique of the following joke: {joke}"
response = await self.llm.acomplete(prompt)
return StopEvent(result=str(response))
# 运行入口
async def main():
w = JokeFlow(timeout=600, verbose=False)
result = await w.run(topic="pirates")
print(str(result))
# run main
if __name__ == '__main__':
asyncio.run(main())
joke:[Why did the pirate quit his job?
Because he was sick of all the arrr-guments.]
A classic play on words!
**Initial Impression**
The joke relies on a clever pun, which is a hallmark of effective humor. The setup "Why did the pirate quit his job?" primes the listener to expect a serious or dramatic reason for the pirate's departure. Instead, the punchline subverts this expectation with a lighthearted and silly twist.
**Analysis**
The joke's success can be attributed to several factors:
1. **Wordplay**: The use of "arrr-guments" is a brilliant play on words, combining the pirate-themed "arrgh" with the word "arguments." This unexpected twist creates surprise and delight.
2. **Misdirection**: The setup misdirects the listener's attention away from the expected reason for the pirate's departure (e.g., boredom, dissatisfaction, etc.) and onto a more trivial aspect of his job (arguments).
3. **Lightheartedness**: The joke's tone is lighthearted and humorous, which helps to diffuse any potential awkwardness or seriousness associated with a pirate quitting their job.
**Critique**
While the joke is well-crafted, there are some minor issues:
1. **Predictability**: Some listeners might find the punchline too predictable or obvious, as it relies on a fairly common type of pun.
2. **Lack of surprise**: The wordplay, while clever, doesn't necessarily create a sense of genuine surprise. A more unexpected twist might make the joke more memorable and impactful.
3. **Limited context**: The joke assumes a certain level of familiarity with pirate culture or language, which might limit its appeal to non-pirate enthusiasts.
**Suggestions for Improvement**
To further enhance this joke, consider adding a few tweaks:
1. **Add more specificity**: Consider adding a specific example or scenario that illustrates the "arrr-guments" aspect of the pirate's job.
2. **Increase the surprise**: You could experiment with more unexpected wordplay or twists to create a greater sense of surprise and delight.
3. **Play with tone**: Experiment with different tones, such as using sarcasm or irony, to add an extra layer of complexity and humor to the joke.
Overall, this joke is well-crafted and relies on clever wordplay, misdirection, and lightheartedness to create a humorous effect. With a few tweaks, it could become even more memorable and effective.
工作流其实并不是一个新的概念,目前已经有很多工作流框架。工作流也可以和大模型结合,可以看到工作流按步骤执行成功了。
53AI,企业落地应用大模型首选服务商
产品:大模型应用平台+智能体定制开发+落地咨询服务
承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业
2024-12-17
llamaindex实战-ChatEngine-Context(上下文)模式
2024-12-01
LlamaIndex,让AI唤醒你的数据
2024-11-29
llamaindex实战-Agent-自定义工具函数
2024-11-22
llamaindex实战-Agent-让Agent调用多个工具函数(本地部署)
2024-11-15
llamaindex实战-Agent-在Agent中使用RAG查询(本地部署)
2024-11-07
深度解析 REAcT Agent 的实现:利用 LlamaIndex 和 Gemini 提升智能代理工作流
2024-11-04
手把手教你用Coze零代码搭建一个智能搜索智能体,高时效性、保姆级!
2024-10-11
深入解析LlamaIndex Workflows【下篇】:实现ReAct模式AI智能体的新方法
2024-07-09
2024-04-20
2024-06-05
2024-04-25
2024-04-28
2024-05-09
2024-07-20
2024-04-26
2024-06-19
2024-04-08