MyShell 백서

MyShell

원문

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 5쪽

utomata", "id": "variable_expression_demo", "initial": "home_page_state", "inputs": {}, "outputs": {}, "context": { "intro_message": "", "tts_widget_id": "", "memory": "" }, "transitions": { "go_home": "home_page_state" }, "states": { "home_page_state": { "inputs": { "intro_message": { "type": "text", "user_input": true, "default_value": "Hi, this is your Pro Config Tutorial Agent, how can I assist you today" }, "tts_widget_id": { "type": "text", "user_input": true, "default_value": "1743159010695057408" } }, "outputs": { "context.intro_message": "{{intro_message}}", "context.tts_widget_id": "{{tts_widget_id}}" }, "render": { "text": "Welcome to this demo. Click 'Start' to chat!", "buttons": [ { "content": "Start Chat", "description": "Click to Start Chatting.", "on_click": "start_chat" } ] }, "transitions": { "start_chat": "intro_message_state" } }, "intro_message_state": { "render": { "text": "{{context.intro_message}}", "buttons": [ { "content": "Home", "description": "Click to Go Back to Home.", "on_click": "go_home" } ] }, "outputs": { "context.memory": "{{[]}}" }, "transitions": { "CHAT": "chat_page_state" } }, "chat_page_state": { "inputs": { "user_message": { "type": "IM", "user_input": true } }, "tasks": [ { "name": "generate_reply", "module_type": "AnyWidgetModule", "module_config": { "widget_id": "1744214024104448000", "system_prompt": "You are a teacher teaching Pro Config.", "user_prompt": "{{user_message}}", "memory": "{{context.memory}}", "output_name": "reply" } }, { "name": "generate_voice", "module_type": "AnyWidgetModule", "module_config": { "content": "{{reply}}", "widget_id": "{{context.tts_widget_id}}", "output_name": "reply_voice" } } ], "outputs": { "context.memory": "{{[...context.memory, {'user': user_message}, {'assistant': reply}]}}" }, "render": { "text": "{{reply}}", "audio": "{{reply_voice}}", "buttons": [ { "content": "Home", "description": "Click to Go Back to Home.", "on_click": "go_home" } ] }, "transitions": { "CHAT": "chat_page_state" } } } } ``` In the above example config, we prompt the user to input `intro_message` and `tts_widget_id`, which are written to `context` in the outputs. These two variables are reused later in `intro_message_state` and `chat_page_state` respectively. Besides, we leverage an array called `memory` to store the chat history and update the memory through an expression (for the grammar of this, follow the [Javascript syntax guide](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide)) : ```json "outputs": { "context.memory": "{{[...memory, {'user': user_message}, {'assistant': reply}]}}" }, ``` which will append the latest chat messages to the memory. Then we pass the memory to the `memory` parameter of `LLMModule` so that the LLM can retain understand how to react based on previous interactions. ### Advanced Data Processing If you want more powerful ability to process data, we recommend using [Code Runner Widget](/create/pro-config-mode/api-reference/module/anywidget-module/code-runner-widget) to execute code snippets in a free manner. It requires knowledge about how to use a widget in Pro Config, which is just to be explained in the next chapter. # Integration with Any Widget MyShell currently supports over a thousand widgets with diverse functionalities, including prompt widgets, voice widgets, image generation widgets, and many more, in which all you could find on our widget center. The AnyWidget Module for Pro Config is designed to be versatile. For instance, constructing a video generation agent that combines ASR, TTS, LipSync, and AutoCaption widgets is within the realm of possibilities. As introduced beforehand, after you get into MyShell's Widget center, there a numerous widgets where you could play with and insert that into your Pro Config. We suggest to follow these steps before you implement it in Pro Config. ### 1. Try it, Hands-on ![](https://2975681513-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDtmZwLatyyBFLUK6P6Pj%2Fuploads%2FSqBVBf7kkzHLYYd0JA3K%2Ftry%20widget.png?alt=media\&token=533e3cad-9b41-4241-a55f-2dc7e2e0d268) ### 2. Adjust the parameters ![](https://2975681513-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDtmZwLatyyBFLUK6P6Pj%2Fuploads%2FK4MCJnDMlhuQ13P9x4fk%2Friffusion_day.png?alt=media\&token=ad3c7cf2-d5ed-4b7c-8efd-9be87d468da0) ### 3. Copy the widget Pro Config template (it's an independent automata that includes that widget as a task) ![](https://2975681513-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDtmZwLatyyBFLUK6P6Pj%2Fuploads%2Fp3Kn8qi1HTuvxkW8E3QE%2Fcopy%20widget%20config.png?alt=media\&token=f1988ab1-3116-4917-9d0b-d57f6d0de22d) ```json { "id": "prompt_widget_template", "initial": "home_state", "states": { "home_state": { "inputs": { "prompt_a": { "type": "text", "description":"The prompt for your audio", "user_input": true } }, "tasks": [ { "name": "any_module_example_task", "module_type": "AnyWidgetModule", "module_config": { "widget_id": "1743838636299784192", "prompt_a":"{{prompt_a}}", // this field will received value from user input "denoising":0.75, // How much to transform input spectrogram "prompt_b":"90's rap", // The second prompt to interpolate with the first, leave blank if no interpolation "alpha":0.5, // Interpolation alpha if using two prompts. A value of 0 uses prompt_a fully, a value of 1 uses prompt_b fully "num_inference_steps":50, // Number of steps to run the diffusion model "seed_image_id":"vibes", // Seed spectrogram to use "output_name": "result" } } ], "render": { "text": "{{JSON.stringify(result)}}", // this widget will output a map, you can first run it and know what its type is. "buttons": [ { "content":"Generate Again", "description":"", "on_click":"generate" } ] }, "transitions": { "generate": "home_state" } } } } ``` ### 4. Modify Pro Config according to the copied config For example, the original Pro Config is the [agent example](/create/pro-config-mode/tutorial/building-workflow#a-simple-chatbot-example) in the previous chapter. We may replace TtsModule with the music generation widget. {% hint style="warning" %} The following JSON is invalid since we just copy the template without changing anything. {% endhint %} ```json { "type": "automata", "id": "chat_demo", "initial": "chat_page_state", "inputs": {}, "outputs": {}, "transitions": {}, "states": { "chat_page_state": { "inputs": { "user_message": { "type": "IM", "user_input": true } }, "tasks": [ { "name": "generate_reply", "module_type": "AnyWidgetModule", "module_config": { "widget_id": "1744214024104448000", "system_prompt": "You are a teacher teaching Pro Config.", "user_prompt": "{{user_message}}", "output_name": "reply" } }, { "name": "any_module_example_task", "module_type": "AnyWidgetModule", "module_config": { "widget_id": "1743838636299784192", "prompt_a":"{{prompt_a}}", // this field will received value from user input "denoising":0.75, // How much to transform input spectrogram "prompt_b":"90's rap", // The second prompt to interpolate with the first, leave blank if no interpolation "alpha":0.5, // Interpolation alpha if using two prompts. A value of 0 uses prompt_a fully, a value of 1 uses prompt_b fully "num_inference_steps":50, // Number of steps to run the diffusion model "seed_image_id":"vibes", // Seed spectrogram to use "output_name": "result" } } ], "render": { "text": "{{reply}}", "audio": "{{reply_voice}}" }, "transitions": { "CHAT": "chat_page_state" } } } } ``` Then the variables' names may be changed to fit the original config. `"prompt_a":"{{prompt_a}}"` needs to be changed to `"prompt_a":"{{reply}}"`. `render` also needs to be changed. {% hint style="info" %} You may find it necessary to add more inputs or outputs to use the widget, because widgets often require parameters you don't anticipate. {% endhint %} ```json { "type": "automata", "id": "chat_demo", "initial": "chat_page_state", "inputs": {}, "outputs": {}, "transitions": {}, "states": { "chat_page_state": { "inputs": { "user_message": { "type": "IM", "user_input": true } }, "tasks": [ { "name": "generate_reply", "module_type": "AnyWidgetModule", "module_config": { "widget_id": "1744214024104448000", "system_prompt": "You are a teacher teaching Pro Config.", "user_prompt": "{{user_message}}", "output_name": "reply" } }, { "name": "any_module_example_task", "module_type": "AnyWidgetModule", "module_config": { "widget_id": "1743838636299784192", "prompt_a":"{{reply}}", // this field will received value from user input "denoising":0.75, // How much to transform input spectrogram "prompt_b":"90's rap", // The second prompt to interpolate with the first, leave blank if no interpolation "alpha":0.5, // Interpolation alpha if using two prompts. A value of 0 uses prompt_a fully, a value of 1 uses prompt_b fully "num_inference_steps":50, // Number of steps to run the diffusion model "seed_image_id":"vibes", // Seed spectrogram to use "output_name": "result" } } ], "render": { "text": "{{JSON.stringify(result)}}", // this widget will output a map, you can first run it and know what its type is. }, "transitions": { "CHAT": "chat_page_state" } } } } ``` Finally, adjust the `module_config` according to the parameters you have tried and decided in the step 2. # An Advanced Example In this chapter, we will implement a more advanced app. This app will start by testing the user's understanding of Pro Config, and then redirect to different pages based on the user's score. If the user's score is low, they will be redirected to a Pro Config tutorial agent. This advanced example includes concepts of Pro Config we have learned in previous chapters and serves as a great starting point for developers who would prefer building an app with more complicated logic. \ \ Here is the config: ```json { "type": "automata", "id": "advanced_example_demo", "initial": "home_page_state", "inputs": {}, "outputs": {}, "context": { "questions_string": "[{\"question\": \"Which of the following statements is not correct? \\n A. The execution of an Automata starts from the `initial` state. \\n B. An Automata can contain multiple AtomicStates. \\n C. Each AtomicState must define both inputs and outputs. \\n D. We can define transitions in either Automata or AtomicState.\", \"answer\": \"C\", \"explanation\": \"Both inputs and outputs in an AtomicState are optional.\"}, {\"question\": \"You are building an AutomicState, please choose the correct order of execution: \\n A. inputs -> tasks -> outputs -> render \\n B. render -> inputs -> tasks -> outputs. \\n C. tasks -> inputs -> outputs -> render. \\n D. render -> tasks -> inputs -> outputs\", \"answer\": \"A\", \"explanation\": \"The correct order is `inputs -> tasks -> outputs -> render`. Please refer to `Expressions and Variables`\"}, {\"question\": \"Which of the following expressions is not correct (assume all the variables exist)? \\n A. context.variable \\n B. variable \\n C. variable1 + variable2 \\n D. np.array(variable)\", \"answer\": \"D\", \"explanation\": \"Our expression supports JavaScript grammar.\"}]", "questions": "", "question_idx": "", "chosen_answer": "", "correct_answer": "", "correct_count": "", "memory": "{{[]}}", "is_correct": "{{false}}", "intro_message": "", "tts_widget_id": "" }, "transitions": { "go_home": "home_page_state", "get_quiz": "quiz_page_state", "continue": "continue_state" }, "states": { "home_page_state": { "inputs": { "intro_message": { "type": "text", "user_input": true, "default_value": "Hi, this is your Pro Config Tutorial Agent, how can I assist you today" }, "tts_widget_id": { "type": "text", "user_input": true, "default_value": "1743159010695057408" } }, "outputs": { "context.intro_message": "{{intro_message}}", "context.tts_widget_id": "{{tts_widget_id}}", "context.questions": "{{JSON.parse(context.questions_string)}}", "context.question_idx": "{{0}}", "context.correct_count": "{{0}}" }, "render": { "text": "Welcome to this Pro Config tutorial agent. Let's start a quiz!", "buttons": [ { "content": "Quiz", "description": "get_quiz", "on_click": "get_quiz" } ] } }, "quiz_page_state": { "outputs": { "context.correct_answer": "{{context.questions[context.question_idx]['answer']}}" }, "render": { "text": "{{context.question_idx + 1}}. {{context.questions[context.question_idx]['question']}}", "buttons": [ { "content": "A.", "description": "Choose A.", "on_click": { "event": "check_answer", "payload": { "chosen_answer": "A" } } }, { "content": "B.", "description": "Choose B.", "on_click": { "event": "check_answer", "payload": { "chosen_answer": "B" } } }, { "content": "C.", "description": "Choose C.", "on_click": { "event": "check_answer", "payload": { "chosen_answer": "C" } } }, { "content": "D.", "description": "Choose D.", "on_click": { "event": "check_answer", "payload": { "chosen_answer": "D" } } } ] }, "transitions": { "check_answer": { "target": "analyze_answer_state", "target_inputs": { "chosen_answer": "{{payload.chosen_answer}}" } } } }, "analyze_answer_state": { "inputs": { "chosen_answer": { "type": "text", "user_input": false } }, "outputs": { "context.chosen_answer": "{{chosen_answer}}", "context.is_correct": "{{chosen_answer == context.correct_answer}}" }, "render": { "text": "Check answer state." }, "transitions": { "ALWAYS": [ { "target": "correct_answer_state", "condition": "{{context.is_correct}}" }, { "target": "wrong_answer_state", "condition": "{{true}}" } ] } }, "correct_answer_state": { "outputs": { "context.question_idx": "{{(context.question_idx + 1) % context.questions.length}}", "context.correct_count": "{{context.correct_count + 1}}" }, "render": { "text": "Congratulations! You have chosen the correct answer {{context.correct_answer}}", "buttons": [ { "content": "Continue", "description": "continue", "on_click": "continue" } ] } }, "wrong_answer_state": { "outputs": { "context.question_idx": "{{(context.question_idx + 1) % context.questions.length}}" }, "render": { "text": "Oh No! The chosen answer is {{context.chosen_answer}}, while the correct one is {{context.correct_answer}}.", "buttons": [ { "content": "Continue", "description": "continue", "on_click": "continue" } ] } }, "continue_state": { "render": { "text": "Click to Next Question" }, "transitions": { "ALWAYS": [ { "target": "quiz_page_state", "condition": "{{context.question_idx > 0}}" }, { "target": "finish_state", "condition": "{{context.correct_count == context.questions.length}}" }, { "target": "review_state", "condition": "{{true}}" } ] } }, "finish_state": { "render": { "text": "Congratulations! You are now a master of Pro Config!", "buttons": [ { "content": "Home", "description": "Back to Home", "on_click": "go_home" } ] } }, "review_state": { "outputs": { "context.memory": "{{[]}}" }, "render": { "text": "{{context.intro_message}}" }, "transitions": { "CHAT": "chat_page_state" } }, "chat_page_state": { "inputs": { "user_message": { "type": "IM", "user_input": true } }, "tasks": [ { "name": "generate_reply", "module_type": "AnyWidgetModule", "module_config": { "widget_id": "1744214024104448000", // GPT-3.5 "system_prompt": "You are a teacher teaching Pro Config. Pro Config is a powerful tool to build AI native applications. Here are some questions and answers about basic concepts of Pro Config: {{context.questions_string}}", "user_prompt": "{{user_message}}", "memory": "{{context.memory}}", "output_name": "reply" } }, { "name": "generate_voice", "module_type": "AnyWidgetModule", "module_config": { "widget_id": "{{context.tts_widget_id}}", "content": "{{reply}}", "output_name": "reply_voice" } } ], "outputs": { "context.memory": "{{[...context.memory, {'user': user_message}, {'assistant': reply}]}}" }, "render": { "text": "{{reply}}", "audio": "{{reply_voice}}", "buttons": [ { "content": "Home", "description": "Click to Go Back to Home.", "on_click": "go_home" } ] }, "transitions": { "CHAT": "chat_page_state" } } } } ``` We now elaborate on the above example by first reviewing some learned concepts, and then introducing some advanced features. ### Overview of the Entire Pipeline Here is the pipeline of the application defined by the above config: * When the app starts, user will be prompted to input the `intro_message` and `tts_widget_id`, which will be passed through the automata and reused later in the chat page. * The user is then directed into a quiz page, where they will be answering several questions about the basic concepts of Pro Config. * If the user gets all questions correct, the app will end, and a congratulation message will pop up. * If the user gets some question wrong, the app will redirect to an agent that can interact and answer questions about Pro Config. <figure><img src="https://2975681513-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDtmZwLatyyBFLUK6P6Pj%2Fuploads%2FGXz5Jn8IrX0zreweJmuQ%2FTutorial%20Bot%20Flow.png?alt=media&amp;token=82b18570-cce6-4b47-8661-e2f1b8e65bf7" alt=""><figcaption><p>This flowchart demonstrates the major state transfer, input/output in each state, and key variable transition. <br>For demo purpose, some states like "continue_state" "review_state" is not drawn out.</p></figcaption></figure> ### Review of Basic Concepts #### Inputs, Outputs, and Render In the above example, we have used two types of inputs `text` and `IM` . `text` inputs prompts the user to input the `intro_message` and `tts_widget_id`. `IM` input is used in the agent where user directly send text messages.&#x20; \ The outputs used in the example are mainly for writing some variables to the context and do some basic calculation (such as increasing the question index). For rendering, we have used text, buttons, and audio in this example. #### Workflow We simply use LLM + TTS as the workflow in the `chat_page_state` ,which has been already discussed in previous chapter. We have fed some basic knowledge of Pro Config to the system\_prompt of the LLM, so that the agent has it's internal knowledge base to answer questions. #### Transitions The above example includes transitions through buttons (controlled by the `on_click` properties) and some reserved action names such as `CHAT` and `ALWAYS` . It also demonstrates how to perform conditional transitions, which we will discuss later. #### Expressions and Variables We can find some basic use cases of expressions (JavaScript grammar) to initialize some variables and do some basic calculation. We have also demonstrated how to use context to pass variables across different states.&#x20; ### Advanced Features #### Conditional Transitions Please refer to `continue_state` which determines the state transition based on user's quiz score. ```json "transitions": { "ALWAYS": [ { "target": "quiz_page_state", "condition": "{{context.question_idx > 0}}" }, { "target": "finish_state", "condition": "{{context.correct_count == context.questions.length}}" }, { "target": "review_state", "condition": "{{true}}" } ] } ``` In this example, it will redirect to the `quiz_page_state` if the `question_idx` is valid. Otherwise, if the user has answered all the questions correctly, it will jump to the `finish_state`. If the user get any question wrong, it will jump to the `review_state` the let the user chat with a Pro Config tutorial agent. The conditions of the transition cases are evaluated sequentially, and the condition in the last case `{{true}}` is similar to the `default` keyword in C++ `case` .&#x
프로젝트로 돌아가기