AI知识库

53AI知识库

学习大模型的前沿技术与行业应用场景


llamaindex实战-Workflow:工作流入门(本地部署断网运行)
发布日期:2024-11-19 12:23:17 浏览次数: 1512 来源:大数据架构师修行之路


介绍

本文介绍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+中大型企业

联系我们

售前咨询
186 6662 7370
预约演示
185 8882 0121

微信扫码

与创始人交个朋友

回到顶部

 
扫码咨询