-这里有一个简单的 Python 示例:
+1. 🧠 **持久 AI 代理**:在长期交互中记住背景和信息。
+2. 💾 **状态会话**:跟踪过去的互动以获得个性化回应。
+3. 🔄 **多步骤任务**:使用循环和决策构建复杂的多步骤流程。
+4. ⏳ **任务管理**:处理可以无限期运行的长时间运行的任务。
+5.🛠️**内置工具**:在您的任务中使用内置工具和外部 API。
+6. 🔧 **自我修复**:Julep 将自动重试失败的步骤、重新发送消息,并确保您的任务顺利运行。
+7. 📚 **RAG**:使用 Julep 的文档存储构建一个用于检索和使用您自己的数据的系统。
-
+Julep 非常适合需要超越简单的提示响应模型的 AI 用例的应用程序。
-
-from julep import Julep, AsyncJulep
+## 为什么选择 Julep 而不是 LangChain?
-# 🔑 初始化 Julep 客户端
-# 或者使用 AsyncJulep 进行异步操作
-client = Julep(api_key="your_api_key")
+### 不同的用例
-##################
-## 🤖 代理 🤖 ##
-##################
+可以将 LangChain 和 Julep 视为 AI 开发堆栈中具有不同重点的工具。
-# 创建一个研究代理
-agent = client.agents.create(
- name="研究代理",
- model="claude-3.5-sonnet",
- about="您是一个设计用于处理研究查询的研究代理。",
-)
-
-# 🔍 为代理添加工具
-client.agents.tools.create(
- agent_id=agent.id,
- name="web_search", # 应该是有效的 Python 变量名
- description="使用此工具进行研究查询。",
- integration={
- "provider": "brave",
- "method": "search",
- "setup": {
- "api_key": "your_brave_api_key",
- },
- },
-)
+LangChain 非常适合创建提示序列和管理与 AI 模型的交互。它拥有庞大的生态系统,包含大量预构建的集成,如果您想快速启动和运行某些功能,这会非常方便。LangChain 非常适合涉及线性提示链和 API 调用的简单用例。
-#################
-## 💬 聊天 💬 ##
-#################
+另一方面,Julep 更注重构建持久的 AI 代理,这些代理可以在长期交互中记住事物。当您需要涉及多个步骤、决策以及在代理流程中直接与各种工具或 API 集成的复杂任务时,它会大放异彩。它从头开始设计,以管理持久会话和复杂任务。
-# 与代理开始交互式聊天会话
-session = client.sessions.create(
- agent_id=agent.id,
- context_overflow="adaptive", # 🧠 Julep 将在需要时动态计算上下文窗口
-)
+如果您想构建一个需要执行以下操作的复杂 AI 助手,请使用 Julep:
-# 🔄 聊天循环
-while (user_input := input("您:")) != "退出":
- response = client.sessions.chat(
- session_id=session.id,
- message=user_input,
- )
+- 跟踪几天或几周内的用户互动。
+- 执行计划任务,例如发送每日摘要或监控数据源。
+- 根据之前的互动或存储的数据做出决策。
+- 作为其任务的一部分,与多个外部服务进行交互。
- print("代理:", response.choices[0].message.content)
+然后 Julep 提供支持所有这些的基础设施,而无需您从头开始构建。
+### 不同的外形尺寸
-#################
-## 📋 任务 📋 ##
-#################
+Julep 是一个**平台**,其中包括用于描述任务的语言、用于运行这些任务的服务器以及用于与平台交互的 SDK。要使用 Julep 构建某些东西,您需要在“YAML”中编写任务描述,然后在云中运行该任务。
-# 为代理创建一个周期性研究任务
-task = client.tasks.create(
- agent_id=agent.id,
- name="研究任务",
- description="每24小时研究给定的主题。",
- #
- # 🛠️ 任务特定工具
- tools=[
- {
- "name": "send_email",
- "description": "向用户发送包含结果的电子邮件。",
- "api_call": {
- "method": "post",
- "url": "https://api.sendgrid.com/v3/mail/send",
- "headers": {"Authorization": "Bearer YOUR_SENDGRID_API_KEY"},
- },
- }
- ],
- #
- # 🔢 任务主要步骤
- main=[
- #
- # 步骤 1:研究主题
- {
- # `_`(下划线)变量指向上一步的输出
- # 这里,它指向用户输入的主题
- "prompt": "查找主题 '{{_.topic}}' 并总结结果。",
- "tools": [{"ref": {"name": "web_search"}}], # 🔍 使用代理的网络搜索工具
- "unwrap": True,
- },
- #
- # 步骤 2:发送包含研究结果的电子邮件
- {
- "tool": "send_email",
- "arguments": {
- "subject": "研究结果",
- "body": "'以下是今天的研究结果:' + _.content",
- "to": "inputs[0].email", # 引用用户输入的电子邮件
- },
- },
- #
- # 步骤 3:等待 24 小时后重复
- {"sleep": "24 * 60 * 60"},
- ],
-)
+Julep 专为繁重、多步骤和长时间运行的任务而设计,并且对任务的复杂程度没有限制。
-# 🚀 启动周期性任务
-client.executions.create(task_id=task.id, input={"topic": "Python"})
+LangChain 是一个**库**,其中包含一些工具和一个用于构建线性提示和工具链的框架。要使用 LangChain 构建某些东西,您通常需要编写 Python 代码来配置和运行要使用的模型链。
-# 🔁 这将每 24 小时运行一次任务,
-# 研究 "Python" 主题,并
-# 将结果发送到用户的电子邮件
-
-
+对于涉及线性提示和 API 调用链的简单用例,LangChain 可能足够并且能够更快地实现。
-## 特性
+### 总之
-Julep 简化了构建具有可定制工作流的持久 AI 代理的过程。主要特性包括:
+当您需要在无状态或短期环境中管理 AI 模型交互和提示序列时,请使用 LangChain。
-- **持久 AI 代理**:创建和管理能够在多次交互中保持上下文的 AI 代理。
-- **可定制工作流**:使用任务(Tasks)设计复杂的多步骤 AI 工作流。
-- **工具集成**:无缝集成各种工具和 API 到您的 AI 工作流中。
-- **文档管理**:高效管理和搜索代理的文档。
-- **会话管理**:处理持久会话以实现连续交互。
-- **灵活执行**:支持工作流中的并行处理、条件逻辑和错误处理。
+当您需要一个具有高级任务功能、持久会话和复杂任务管理的状态代理的强大框架时,请选择 Julep。
-## 安装
+## 安装
-要开始使用 Julep,请使用 [npm](https://www.npmjs.com/package/@julep/sdk) 或 [pip](https://pypi.org/project/julep/) 安装:
+要开始使用 Julep,请使用 [npm](https://www.npmjs.com/package/@julep/sdk) 或 [pip](https://pypi.org/project/julep/) 安装它:
```bash
npm install @julep/sdk
```
-或
+或者
```bash
pip install julep
```
-> [!TIP]
-> 在测试阶段,您可以通过 [Discord](https://discord.com/invite/JTSBGRZrzj) 获取 API 密钥。
-
-## 快速入门指南
+> [!注意]
+> 从[此处](https://dashboard-dev.julep.ai)获取您的 API 密钥。
+>
+> 虽然我们处于测试阶段,但您也可以通过 [Discord](https://discord.com/invite/JTSBGRZrzj) 联系,以解除 API 密钥的速率限制。
-### 步骤 1:导入 Julep
+> [!提示]
+> 💻 你是“向我展示代码!”的那种人吗?我们创建了大量的烹饪书供您入门。**查看 [烹饪书](https://github.com/julep-ai/julep/tree/dev/cookbooks)** 以浏览示例。
+>
+> 💡 您还可以在 Julep 的基础上构建许多想法。**查看[想法列表](https://github.com/julep-ai/julep/tree/dev/cookbooks/IDEAS.md)** 以获取一些灵感。
-首先,将 Julep SDK 导入到您的项目中:
+## Python 快速入门🐍
-```javascript
-const Julep = require('@julep/sdk');
-```
-
-或
+### 步骤 1:创建代理
```python
-from julep import AsyncJulep
-```
+import yaml
+from julep import Julep # or AsyncJulep
-### 步骤 2:初始化代理
+client = Julep(api_key="your_julep_api_key")
-使用基本设置创建一个新代理:
-
-```javascript
-const julep = new Julep({ apiKey: 'your-api-key' });
+agent = client.agents.create(
+ name="Storytelling Agent",
+ model="gpt-4o",
+ about="You are a creative storytelling agent that can craft engaging stories and generate comic panels based on ideas.",
+)
-const agent = await julep.agents.create({
- name: '研究助手',
- model: 'gpt-4-turbo',
- about: "您是一个创意讲故事代理,能够根据想法创作引人入胜的故事并生成漫画面板。",
-});
+# 🛠️ Add an image generation tool (DALL·E) to the agent
+client.agents.tools.create(
+ agent_id=agent.id,
+ name="image_generator",
+ description="Use this tool to generate images based on descriptions.",
+ integration={
+ "provider": "dalle",
+ "method": "generate_image",
+ "setup": {
+ "api_key": "your_openai_api_key",
+ },
+ },
+)
```
-或
+### 步骤 2:创建一个生成故事和漫画的任务
+
+让我们定义一个多步骤任务来创建一个故事并根据输入的想法生成面板漫画:
```python
-client = AsyncJulep(api_key="your_api_key")
+# 📋 Task
+# Create a task that takes an idea and creates a story and a 4-panel comic strip
+task_yaml = """
+name: Story and Comic Creator
+description: Create a story based on an idea and generate a 4-panel comic strip illustrating the story.
+
+main:
+ # Step 1: Generate a story and outline into 4 panels
+ - prompt:
+ - role: system
+ content: You are {{agent.name}}. {{agent.about}}
+ - role: user
+ content: >
+ Based on the idea '{{_.idea}}', write a short story suitable for a 4-panel comic strip.
+ Provide the story and a numbered list of 4 brief descriptions for each panel illustrating key moments in the story.
+ unwrap: true
+
+ # Step 2: Extract the panel descriptions and story
+ - evaluate:
+ story: _.split('1. ')[0].strip()
+ panels: re.findall(r'\\d+\\.\\s*(.*?)(?=\\d+\\.\\s*|$)', _)
+
+ # Step 3: Generate images for each panel using the image generator tool
+ - foreach:
+ in: _.panels
+ do:
+ tool: image_generator
+ arguments:
+ description: _
+
+ # Step 4: Generate a catchy title for the story
+ - prompt:
+ - role: system
+ content: You are {{agent.name}}. {{agent.about}}
+ - role: user
+ content: >
+ Based on the story below, generate a catchy title.
+
+ Story: {{outputs[1].story}}
+ unwrap: true
+
+ # Step 5: Return the story, the generated images, and the title
+ - return:
+ title: outputs[3]
+ story: outputs[1].story
+ comic_panels: "[output.image.url for output in outputs[2]]"
+"""
-agent = await client.agents.create(
- name="讲故事代理",
- model="gpt-4-turbo",
- about="您是一个创意讲故事代理,能够根据想法创作引人入胜的故事并生成漫画面板。",
+task = client.tasks.create(
+ agent_id=agent.id,
+ **yaml.safe_load(task_yaml)
)
```
-### 步骤 3:与代理聊天
-
-与代理开始交互式聊天会话:
+### 步骤 3:执行任务
-```javascript
-const session = await julep.sessions.create({
- agentId: agent.id,
-});
+```python
+# 🚀 Execute the task with an input idea
+execution = client.executions.create(
+ task_id=task.id,
+ input={"idea": "A cat who learns to fly"}
+)
-// 向代理发送消息
-const response = await julep.sessions.chat({
- sessionId: session.id,
- message: '你好,能给我讲个故事吗?',
-});
+# 🎉 Watch as the story and comic panels are generated
+for transition in client.executions.transitions.stream(execution_id=execution.id):
+ print(transition)
-console.log(response);
+# 📦 Once the execution is finished, retrieve the results
+result = client.executions.get(execution_id=execution.id)
```
-或
+### 步骤 4:与代理聊天
+
+开始与代理进行交互式聊天会话:
```python
-session = await client.sessions.create(agent_id=agent.id)
+session = client.sessions.create(agent_id=agent.id)
-# 向代理发送消息
-response = await client.sessions.chat(
- session_id=session.id,
- message="你好,能给我讲个故事吗?",
-)
+# 💬 Send messages to the agent
+while (message := input("Enter a message: ")) != "quit":
+ response = client.sessions.chat(
+ session_id=session.id,
+ message=message,
+ )
-print(response)
+ print(response)
```
-### 步骤 4:创建多步骤任务
+> [!提示]
+> 您可以在[这里](example.py)找到完整的 python 示例。
-让我们定义一个多步骤任务,根据输入的想法创建故事并生成分镜漫画:
-```python
-# 🛠️ 为代理添加图像生成工具(DALL·E)
-await client.agents.tools.create(
- agent_id=agent.id,
- name="image_generator",
- description="使用此工具根据描述生成图像。",
- integration={
- "provider": "dalle",
- "method": "generate_image",
- "setup": {
- "api_key": "your_dalle_api_key",
- },
+## Node.js 快速入门 🟩
+
+### 步骤 1:创建代理
+
+```javascript
+import { Julep } from '@julep/sdk';
+import yaml from 'js-yaml';
+
+const client = new Julep({ apiKey: 'your_julep_api_key' });
+
+async function createAgent() {
+ const agent = await client.agents.create({
+ name: "Storytelling Agent",
+ model: "gpt-4",
+ about: "You are a creative storytelling agent that can craft engaging stories and generate comic panels based on ideas.",
+ });
+
+ // 🛠️ Add an image generation tool (DALL·E) to the agent
+ await client.agents.tools.create(agent.id, {
+ name: "image_generator",
+ description: "Use this tool to generate images based on descriptions.",
+ integration: {
+ provider: "dalle",
+ method: "generate_image",
+ setup: {
+ api_key: "your_openai_api_key",
+ },
},
-)
+ });
-# 📋 任务
-# 创建一个任务,接受一个想法并创建故事和 4 格漫画
-task = await client.tasks.create(
- agent_id=agent.id,
- name="故事和漫画创作器",
- description="根据一个想法创作故事并生成 4 格漫画来说明故事。",
- main=[
- # 步骤 1:生成故事并将其概括为 4 个面板
- {
- "prompt": [
- {
- "role": "system",
- "content": "您是 {{agent.name}}。{{agent.about}}"
- },
- {
- "role": "user",
- "content": (
- "基于想法 '{{_.idea}}',写一个适合 4 格漫画的短故事。"
- "提供故事和一个编号列表,包含 4 个简短描述,每个描述对应一个面板,说明故事中的关键时刻。"
- ),
- },
- ],
- "unwrap": True,
- },
- # 步骤 2:提取面板描述和故事
- {
- "evaluate": {
- "story": "_.split('1. ')[0].strip()",
- "panels": "re.findall(r'\\d+\\.\\s*(.*?)(?=\\d+\\.\\s*|$)', _)",
- }
- },
- # 步骤 3:使用图像生成器工具为每个面板生成图像
- {
- "foreach": {
- "in": "_.panels",
- "do": {
- "tool": "image_generator",
- "arguments": {
- "description": "_",
- },
- },
- },
- },
- # 步骤 4:为故事生成一个吸引人的标题
- {
- "prompt": [
- {
- "role": "system",
- "content": "您是 {{agent.name}}。{{agent.about}}"
- },
- {
- "role": "user",
- "content": "根据以下故事,生成一个吸引人的标题。\n\n故事:{{outputs[1].story}}",
- },
- ],
- "unwrap": True,
- },
- # 步骤 5:返回故事、生成的图像和标题
- {
- "return": {
- "title": "outputs[3]",
- "story": "outputs[1].story",
- "comic_panels": "[output.image.url for output in outputs[2]]",
- }
- },
- ],
-)
+ return agent;
+}
```
-> [!TIP]
-> Node.js 版本的代码类似。
+### 步骤 2:创建一个生成故事和漫画的任务
-### 步骤 5:执行任务
+```javascript
+const taskYaml = `
+name: Story and Comic Creator
+description: Create a story based on an idea and generate a 4-panel comic strip illustrating the story.
+
+main:
+ # Step 1: Generate a story and outline into 4 panels
+ - prompt:
+ - role: system
+ content: You are {{agent.name}}. {{agent.about}}
+ - role: user
+ content: >
+ Based on the idea '{{_.idea}}', write a short story suitable for a 4-panel comic strip.
+ Provide the story and a numbered list of 4 brief descriptions for each panel illustrating key moments in the story.
+ unwrap: true
+
+ # Step 2: Extract the panel descriptions and story
+ - evaluate:
+ story: _.split('1. ')[0].trim()
+ panels: _.match(/\\d+\\.\\s*(.*?)(?=\\d+\\.\\s*|$)/g)
+
+ # Step 3: Generate images for each panel using the image generator tool
+ - foreach:
+ in: _.panels
+ do:
+ tool: image_generator
+ arguments:
+ description: _
+
+ # Step 4: Generate a catchy title for the story
+ - prompt:
+ - role: system
+ content: You are {{agent.name}}. {{agent.about}}
+ - role: user
+ content: >
+ Based on the story below, generate a catchy title.
+
+ Story: {{outputs[1].story}}
+ unwrap: true
+
+ # Step 5: Return the story, the generated images, and the title
+ - return:
+ title: outputs[3]
+ story: outputs[1].story
+ comic_panels: outputs[2].map(output => output.image.url)
+`;
+
+async function createTask(agent) {
+ const task = await client.tasks.create(agent.id, yaml.load(taskYaml));
+ return task;
+}
+```
-```python
-# 🚀 执行任务,输入一个想法
-execution = await client.executions.create(
- task_id=task.id,
- input={"idea": "一只学会飞翔的猫"}
-)
+### 步骤 3:执行任务
-# 🎉 观看故事和漫画面板的生成过程
-await client.executions.stream(execution_id=execution.id)
+```javascript
+async function executeTask(task) {
+ const execution = await client.executions.create(task.id, {
+ input: { idea: "A cat who learns to fly" }
+ });
+
+ // 🎉 Watch as the story and comic panels are generated
+ for await (const transition of client.executions.transitions.stream(execution.id)) {
+ console.log(transition);
+ }
+
+ // 📦 Once the execution is finished, retrieve the results
+ const result = await client.executions.get(execution.id);
+ return result;
+}
```
-这个例子展示了如何创建一个带有自定义工具的代理,定义一个复杂的多步骤任务,并执行它以生成创意输出。
+### 步骤 4:与代理聊天
-
+```javascript
+async function chatWithAgent(agent) {
+ const session = await client.sessions.create({ agent_id: agent.id });
+
+ // 💬 Send messages to the agent
+ const rl = readline.createInterface({
+ input: process.stdin,
+ output: process.stdout
+ });
+
+ const chat = async () => {
+ rl.question("Enter a message (or 'quit' to exit): ", async (message) => {
+ if (message.toLowerCase() === 'quit') {
+ rl.close();
+ return;
+ }
+
+ const response = await client.sessions.chat(session.id, { message });
+ console.log(response);
+ chat();
+ });
+ };
+
+ chat();
+}
+
+// Run the example
+async function runExample() {
+ const agent = await createAgent();
+ const task = await createTask(agent);
+ const result = await executeTask(task);
+ console.log("Task Result:", result);
+ await chatWithAgent(agent);
+}
+
+runExample().catch(console.error);
+```
-> [!TIP]
-> 您可以在[这里](example.ts)找到另一个 Node.js 示例,或在[这里](example.py)找到 Python 示例。
+> [!提示]
+> 您可以在[这里](example.js)找到完整的 Node.js 示例。
-## 概念
+## 成分
-Julep 建立在几个关键的技术组件之上,这些组件协同工作以创建强大的 AI 工作流:
+Julep 由以下成分组成:
-### 代理
-由大型语言模型(LLM)支持的 AI 实体,执行任务并与用户交互。代理是 Julep 的核心功能单元。
+- **Julep 平台**:Julep 平台是运行您的工作流程的云服务。它包括用于描述工作流程的语言、用于运行这些工作流程的服务器以及用于与平台交互的 SDK。
+- **Julep SDKs**:Julep SDKs 是一组用于构建工作流的库。目前有适用于 Python 和 JavaScript 的 SDKs,还有更多 SDKs 正在开发中。
+- **Julep API**:Julep API 是一个 RESTful API,您可以使用它与 Julep 平台进行交互。
-```mermaid
-graph TD
- Agent[代理] --> LLM[大型语言模型]
- Agent --> Tasks[任务]
- Agent --> Users[用户]
- Tasks --> Tools[工具]
-```
+### 心智模型
-### 用户
-与代理交互的实体。用户可以与会话关联,并拥有自己的元数据,允许个性化交互。
+