<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kun-Lung Wu on Medium]]></title>
        <description><![CDATA[Stories by Kun-Lung Wu on Medium]]></description>
        <link>https://medium.com/@kwu888?source=rss-f9d7c2b6b221------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*f3_9DiYLw5gNxwx17UC_-Q.jpeg</url>
            <title>Stories by Kun-Lung Wu on Medium</title>
            <link>https://medium.com/@kwu888?source=rss-f9d7c2b6b221------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 09:35:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kwu888/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[AI-Driven Support Desk with Ray Workflow HTTP Events]]></title>
            <link>https://medium.com/@kwu888/ai-driven-support-desk-with-ray-workflow-http-events-453063b1ca17?source=rss-f9d7c2b6b221------2</link>
            <guid isPermaLink="false">https://medium.com/p/453063b1ca17</guid>
            <category><![CDATA[ai-driven-workflow]]></category>
            <category><![CDATA[workflow-http-events]]></category>
            <category><![CDATA[ray-workflow]]></category>
            <dc:creator><![CDATA[Kun-Lung Wu]]></dc:creator>
            <pubDate>Tue, 23 Aug 2022 14:00:10 GMT</pubDate>
            <atom:updated>2022-08-23T14:00:10.874Z</atom:updated>
            <content:encoded><![CDATA[<p>We describe an AI-driven workflow application to automatically dispatch an IT support ticket to the proper support team, based on a text classifier applied to the ticket description. Once the ticket is dispatched to a support team, the workflow waits asynchronously for the resolution from the team. Depending on the returned resolution, the ticket can be escalated further to an L2 support team, or simply closed if it is resolved successfully. Note that the usage patterns described in this blog are general and applicable to many other technical support desks, in addition to IT support.</p><h4>Why Ray Workflow?</h4><p><a href="https://docs.ray.io/en/latest/workflows/index.html">Ray Workflow</a> combines the flexibility of Ray’s dynamic task graphs with strong durability guarantees, which are generally required for many business workflows. It allows dynamic branching or looping conditionally based on runtime data. It scales naturally with the size of the cluster. More importantly, it offers the potential for seamlessly integration of AI/ML pipelines with general business workflows.</p><p>In Ray 2.0, Workflow supports sending external events via HTTP. In this blog, we use HTTP events as an example to build an AI-driven support desk. The HTTP event support broadens the scope of Ray Workflow from machine learning pipelines to general business workflows that may be asynchronously triggered by external HTTP events, bringing together AI/ML intelligence with business workflows to empower AI-driven business automation.</p><h4>Major Tasks for an IT Support Desk</h4><p>Major tasks for an IT support desk usually involve ticket triage, ticket dispatching and ticket resolution. Depending on the solution returned from the responding team, the ticket may be further escalated to an L2 team if it is not resolved satisfactorily. Otherwise, the ticket is resolved and closed. Note that escalation may repeat multiple times if the ticket is still not resolved. The image below shows the typical workflow of these tasks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b4jz0Luov0bMXbzBuXfmag.png" /></figure><h4>Important <a href="https://docs.ray.io/en/latest/workflows/key-concepts.html">Concepts of Ray Workflow</a> in Ray 2.0</h4><p><strong>Ray DAG:</strong> Ray tasks are generally executed eagerly. In Ray 2.0, Ray DAG provides a way to build the DAG without execution. Each Workflow task represents a node in the Workflow DAG. Ray Workflow is based on Ray DAGs and provides the runtime to execute them.</p><p><strong>Workflows:</strong> A workflow’s execution is durably logged to storage. Upon a system failure, workflows can be resumed on any Ray cluster with access to the storage. When executing the workflow DAG, workflow tasks are retried on failure, but once they finish successfully and the results are persisted by the workflow engine, they will never be run again.</p><p><strong>Objects:</strong> Data objects can be stored in the Ray object store. References to these objects can be passed into and returned from Workflow tasks. Objects are checkpointed when initially returned from a task. After checkpointing, the object can be shared among workflow tasks at memory-speed via the Ray object store.</p><p><strong>Dynamic Workflows:</strong> Workflows can generate new Workflow tasks at runtime. This is achieved by returning a continuation of a Workflow DAG.</p><p><strong>Events:</strong> Workflow tasks can be triggered by timers or external events using the event system, such as HTTP events.</p><h4>Design an IT Support Desk with Ray <a href="https://docs.ray.io/en/latest/workflows/events.html#http-events">Workflow HTTP Events</a></h4><p>To use HTTP events, we need to import HTTPListener from the ray.workflow.http_event_providermodule.</p><pre>import ray<br>from ray import workflow, serve<br>from ray.workflow.http_event_provider import HTTPListener</pre><h4>Define a Ray Workflow Task</h4><p>A workflow task can be defined as a remote Ray task. In the ticket_triage(ticket) task, a classifier can be used to do a text classification based on the text description of the ticket. The triage task then returns the proper support team to service this ticket.</p><pre>@ray.remote<br>def ticket_triage(ticket:str):<br>    def invoke_text_classification(ticket):<br>        # a placeholder to drop in a classifier here to assign the<br>        # ticket to a proper support team based on the ticket<br>        # description.<br>        if &#39;Windows&#39; in &#39;&#39;.join(ticket):<br>            return &#39;Windows_support&#39;<br>        elif &#39;MacOS&#39; in &#39;&#39;.join(ticket):<br>            return &#39;MacOS_support&#39;<br>    return invoke_text_classification(ticket)</pre><h4>Use workflow.wait_for_event()</h4><p>In Ray Workflow, <a href="https://docs.ray.io/en/latest/workflows/events.html">workflow events</a> are special workflow tasks, which “finish” when the events occur. The workflow.wait_for_event() API can be used to create an event task. The ticket_dispatching(team) task illustrates the use of this API to wait for and receive asynchronously the solution returned from the support team. Here a workflow.wait_for_event(HTTPListener, event_key=team) is created to use HTTP events to receive the solution from an external sender — namely the support team working on the ticket. Once the event occurs, the solution is returned.</p><pre>@ray.remote<br>def ticket_dispatching(team:str):<br>    # email support personnel<br>    print(f&quot;^^^^^ ticket dispatched to: {team}&quot;)<br>    return workflow.wait_for_event(HTTPListener, event_key=team)</pre><h4>Use workflow.continuation() for <a href="https://docs.ray.io/en/latest/workflows/basics.html#dynamic-workflows">dynamic workflows</a></h4><p>The solution from the workflow.wait_for_event(HTTPListener, event_key=team) is passed to the ticket_resolution(solution) task (see below). Depending on the solution, the ticket might be escalated to an L2 support team if it is not resolved satisfactorily. In this case, a workflow.continuation(ticket_resolution.bind(ticket_escalation.bind(solution))) is returned. Note that workflow.continuation() API is used to generate a dynamic continuation of another workflow DAG. A continuation is something returned by a function and executed after it returns. Namely, the ticket_escalation() task is executed after the ticket_resolution() task returns. Note that after ticket_escalation() returns, the solution needs to feed into ticket_resolution() again to see if the ticket is resolved. Note that Ray Workflow provides .bind() to define a node in a workflow DAG from a function task.</p><pre>@ray.remote<br>def ticket_resolution(solution:str):<br>    if &#39;Need L2 support&#39; in &#39;&#39;.join(solution):<br>        return workflow.continuation(ticket_resolution.bind(ticket_escalation.bind(solution)))<br>    else:<br>        return solution</pre><pre>@ray.remote<br>def ticket_escalation(ticket_history:str):<br>    print(f&quot;^^^^^ ticket escalated: {ticket_history}&quot;)<br>    return workflow.wait_for_event(HTTPListener, event_key=&#39;L2_support&#39;)</pre><h4>Compose a Ray Workflow DAG</h4><p>The entire IT support desk workflow DAG can be composed using the InputNode() from ray.dag module.</p><pre>from ray.dag import InputNode<br><br>with InputNode() as dag_input:<br>    x = ticket_triage.bind(dag_input.ticket)<br>    y = ticket_dispatching.bind(x)<br>    z = ticket_resolution.bind(y)</pre><h4>Run a Ray Workflow DAG</h4><p>A ticket can be used to trigger a workflow execution. The ticket description text can be used to drive the workflow. Here we show two tickets are used to trigger the running of two different workflows.</p><pre>result_objref_1 = workflow.run_async(z, workflow_id=&#39;support_10001&#39;, ticket=&#39;Windows failed in opening Word application&#39;)<br>result_objref_2 = workflow.run_async(z, workflow_id=&#39;support_10002&#39;, ticket=&#39;MacOS conflicting with Python 3.8&#39;)</pre><h4>Send an HTTP Request</h4><p>An external source, e.g., a support team, can send an HTTP event to the workflow via an HTTP endpoint. The Ray Workflow uses a Ray Serve deployment to provide such an HTTP endpoint. For details about the URL, please see the Ray <a href="https://docs.ray.io/en/latest/workflows/events.html#http-events">Workflow HTTP events</a>. Generally, an HTTP endpoint at http://hostname:port/event/send_event/&lt;workflow_id&gt; can be used to send an event.The example below shows running the Ray Serve deployment locally and it can be reached at <a href="http://127.0.0.1:8000/event/send_event.">http://127.0.0.1:8000/event/send_event/&lt;workflow_id&gt;.</a> To send an event, the workflow_id is required to be part of the HTTP request URL, and a JSON request body needs to provide two key-value pairs: event_key and event_payload(see below).</p><pre>def send_event(workflow_id: str, key: str, payload:str):<br>    res = requests.post(<br>        &quot;http://127.0.0.1:8000/event/send_event/&quot;<br>        + workflow_id,<br>        json={&quot;event_key&quot;: key, &quot;event_payload&quot;: payload},<br>    )<br>    return res</pre><p>For example, the ticket serviced by the workflow_id of “support_10001” is first dispatched to a Windows_support team, but the problem is too complex and needs an L2 support. The ticket will be escalated and sent to an L2 team, which then will resolve the problem and send its solution back. In contrast, the MacOS_support team sends an HTTP event to workflow_id of “support_10002” with a message indicating the ticket is resolved.</p><pre>status = send_event(&quot;support_10001&quot;, &quot;Windows_support&quot;, &quot;ticket is complex. Need L2 support signed by David&quot;)<br>status = send_event(&quot;support_10001&quot;, &quot;L2_support&quot;, &quot;ticket resolved. signed by Mary&quot;)<br>status = send_event(&quot;support_10002&quot;, &quot;MacOS_support&quot;, &quot;ticket resolved, signed by Joe&quot;)</pre><p>The full code described in this blog is stored in a file, support_desk_ray_workflow_http_events.py, and can be downloaded from <a href="https://gist.github.com/klwuibm/0e5afe2e38e0070ba947152ae54901e0">here</a><strong>. </strong>To run it, simply type“python support_desk_ray_workflow_http_events.py”, assuming you have ray 2.0 already installed.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=453063b1ca17" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tuning hyper-parameters with CodeFlare Pipelines]]></title>
            <link>https://medium.com/codeflare/tuning-hyper-parameters-with-codeflare-pipelines-13b884c8336f?source=rss-f9d7c2b6b221------2</link>
            <guid isPermaLink="false">https://medium.com/p/13b884c8336f</guid>
            <category><![CDATA[codeflare-pipelines]]></category>
            <category><![CDATA[codeflare]]></category>
            <category><![CDATA[gridsearchcv]]></category>
            <dc:creator><![CDATA[Kun-Lung Wu]]></dc:creator>
            <pubDate>Tue, 22 Jun 2021 03:01:59 GMT</pubDate>
            <atom:updated>2021-06-22T03:01:59.976Z</atom:updated>
            <content:encoded><![CDATA[<p>GridSearchCV() is often used for hyper-parameter turning for a model constructed via sklearn pipelines. It does an exhaustive search over specified parameter values for a pipeline. It implements a fit() method and a score() method. The parameters of the pipeline used to apply these methods are optimized by cross-validated grid-search over a parameter grid.</p><p>Here we show how to convert an example of using GridSearchCV() to tune the hyper-parameters of an sklearn pipeline into one that uses Codeflare (CF) pipelines grid_search_cv(). We use the “<a href="https://scikit-learn.org/stable/auto_examples/compose/plot_digits_pipe.html#sphx-glr-auto-examples-compose-plot-digits-pipe-py"><strong>Pipelining: chaining a PCA and a logistic regression</strong></a>” from sklearn pipelines as an example.</p><p>In this sklearn example, a pipeline is chained together with a PCA and a LogisticRegression. The n_components parameter of the PCA and the C parameter of the LogisticRegression are defined in a param_grid: with n_components in [5, 15, 30, 45, 64] and C defined by <a href="https://numpy.org/doc/stable/reference/generated/numpy.logspace.html#numpy.logspace">np.logspace</a>(-4, 4, 4). A total of 20 combinations of n_components and C parameter values will be explored by GridSearchCV() to find the best one with the highest mean_test_score.</p><pre>pca = <a href="https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html#sklearn.decomposition.PCA">PCA</a>()<br>logistic = <a href="https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression">LogisticRegression</a>(max_iter=10000, tol=0.1)<br>pipe = <a href="https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.Pipeline.html#sklearn.pipeline.Pipeline">Pipeline</a>(steps=[(&#39;pca&#39;, pca), (&#39;logistic&#39;, logistic)])<br><br>X_digits, y_digits = <a href="https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_digits.html#sklearn.datasets.load_digits">datasets.load_digits</a>(return_X_y=True)<br><br>param_grid = {<br>    &#39;pca__n_components&#39;: [5, 15, 30, 45, 64],<br>    &#39;logistic__C&#39;: <a href="https://numpy.org/doc/stable/reference/generated/numpy.logspace.html#numpy.logspace">np.logspace</a>(-4, 4, 4),<br>}<br>search = <a href="https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html#sklearn.model_selection.GridSearchCV">GridSearchCV</a>(pipe, param_grid, n_jobs=-1)<br>search.fit(X_digits, y_digits)<br>print(&quot;Best parameter (CV score=%0.3f):&quot; % search.best_score_)<br>print(search.best_params_)</pre><p>After running GridSearchCV().fit(), the best parameters of PCA__n_components and LogisticRegression__C, together with the cross-validated mean_test scores are printed out as follows. In this example, the best n_components chosen is 45 for the PCA.</p><pre>Best parameter (CV score=0.920):<br>{&#39;logistic__C&#39;: 0.046415888336127774, &#39;pca__n_components&#39;: 45}</pre><p>The PCA explained variance ratio and the best n_components chosen are plotted in the top chart. The classification accuracy and its std_test_score are plotted in the bottom chart. The best n_components can be obtained by calling best_estimator_.named_step[‘pca’].n_components from the returned object of GridSearchCV().</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/424/1*XZAEz1toi42SqAB0pmGi9w.png" /></figure><h3>Converting to CF pipelines grid_search_cv()</h3><p>We next describe the step-by-step conversion of this example to one that uses Codeflare pipelines.</p><h4><strong>Step 1: importing codeflare.pipelines packages and ray</strong></h4><p>We need to first import various codeflare.pipelines packages, including Datamodel and runtime, as well as ray and call ray.shutdwon() and ray.init(). Note that, in order to run this codeflare example notebook, you need to have a running ray instance.</p><pre>import codeflare.pipelines.Datamodel as dm<br>import codeflare.pipelines.Runtime as rt<br>from codeflare.pipelines.Datamodel import Xy<br>from codeflare.pipelines.Datamodel import XYRef<br>from codeflare.pipelines.Runtime import ExecutionType</pre><pre>import ray<br>ray.shutdown()<br>ray.init()</pre><h4>Step 2: defining and setting up a codeflare pipeline</h4><p>A codeflare pipeline is defined by EstimatorNodes and edges connecting two EstimatorNodes. In this case, we define node_pca and node_logistic and we connect these two nodes with pipeline.add_edge(). Before we can execute fit() on a pipeline, we need to set up the proper input to the pipeline.</p><pre>pca = PCA()<br># set the tolerance to a large value to make the example faster<br>logistic = LogisticRegression(max_iter=10000, tol=0.1)<br><br>pipeline = dm.Pipeline()<br>node_pca = dm.EstimatorNode(&#39;pca&#39;, pca)<br>node_logistic = dm.EstimatorNode(&#39;logistic&#39;, logistic)<br><br>pipeline.add_edge(node_pca, node_logistic)<br><br># input to pipeline<br>pipeline_input = dm.PipelineInput()<br>pipeline_input.add_xy_arg(node_pca, dm.Xy(X_digits, y_digits))</pre><h4>Step 3: defining pipeline param grid and executing Codeflare pipelines grid_search_cv()</h4><p>Codeflare pipelines runtime converts an sklearn param_grid into a codeflare pipelines param grid. We also specify the default KFold parameter for running the cross-validation. Finally, Codeflare pipelines runtime executes the grid_search_cv().</p><pre># param_grid<br>param_grid = {<br>        &#39;pca__n_components&#39;: [5, 15, 30, 45, 64],<br>        &#39;logistic__C&#39;: np.logspace(-4, 4, 4),<br>    }<br><br>pipeline_param = dm.PipelineParam.from_param_grid(param_grid)<br><br># default KFold for grid search<br>k = 5<br>kf = KFold(k)<br><br># execute CF pipeplie grid_search_cv<br>result = rt.grid_search_cv(kf, pipeline, pipeline_input, pipeline_param)</pre><h4>Step 4: parsing the returned result from grid_search_cv()</h4><p>As the Codeflare pipelines project is still actively under development, APIs to access some attributes of the explored pipelines in the grid_search_cv() are not yet available. As a result, a slightly more verbose code is used to get the best pipeline, its associated parameter values and other statistics from the returned object of grid_search_cv(). For example, we need to loop through all the 20 explored pipelines to get the best pipeline. And, to get the n_component of an explored pipeline, we first use .get_nodes() on the returned cross-validated pipeline and then use .get_estimator() and then finally use .get_params().</p><pre>import statistics<br><br># pick the best mean&#39; and best pipeline<br>best_pipeline = None<br>best_mean_scores = 0.0<br>best_n_components = 0<br><br>df = pd.DataFrame(columns =(&#39;n_components&#39;, &#39;mean_test_score&#39;, &#39;std_test_score&#39;))<br><br>for cv_pipeline, scores in result.items():<br>    mean = statistics.mean(scores)<br>    std = statistics.stdev(scores)<br>    n_components = 0<br>    params = {}<br>    # get the &#39;n_components&#39; value of the PCA in this cv_pipeline<br>    for node_name, node in cv_pipeline.get_nodes().items():<br>        params[node_name] = node.get_estimator().get_params()<br>        if &#39;n_components&#39; in params[node_name]:<br>            n_components = params[node_name][&#39;n_components&#39;]<br>    assert(n_components &gt; 0)<br>    df = df.append({&#39;n_components&#39; : n_components, &#39;mean_test_score&#39; : mean, &#39;std_test_score&#39; : std}, ignore_index=True)<br>    if mean &gt; 0.92:<br>        print(mean)<br>        print(str(params))<br>        <br>    if mean &gt; best_mean_scores:<br>        best_pipeline = cv_pipeline<br>        best_mean_scores = mean<br>        best_n_components = n_components</pre><p>Due to the differences in split, the Codeflare pipelines grid_search_cv() produces the best pipeline with n_components = 64 for the PAC, and the 2nd best with n_components = 45. We print out the parameters of the 2nd best and the best pipelines as follows.</p><pre>0.9226679046734757<br>{&#39;pca__3&#39;: {&#39;copy&#39;: True, &#39;iterated_power&#39;: &#39;auto&#39;, &#39;n_components&#39;: 45, &#39;random_state&#39;: None, &#39;svd_solver&#39;: &#39;auto&#39;, &#39;tol&#39;: 0.0, &#39;whiten&#39;: False}, &#39;logistic__1&#39;: {&#39;C&#39;: 0.046415888336127774, &#39;class_weight&#39;: None, &#39;dual&#39;: False, &#39;fit_intercept&#39;: True, &#39;intercept_scaling&#39;: 1, &#39;l1_ratio&#39;: None, &#39;max_iter&#39;: 10000, &#39;multi_class&#39;: &#39;auto&#39;, &#39;n_jobs&#39;: None, &#39;penalty&#39;: &#39;l2&#39;, &#39;random_state&#39;: None, &#39;solver&#39;: &#39;lbfgs&#39;, &#39;tol&#39;: 0.1, &#39;verbose&#39;: 0, &#39;warm_start&#39;: False}}<br>0.9260058805323429<br>{&#39;pca__4&#39;: {&#39;copy&#39;: True, &#39;iterated_power&#39;: &#39;auto&#39;, &#39;n_components&#39;: 64, &#39;random_state&#39;: None, &#39;svd_solver&#39;: &#39;auto&#39;, &#39;tol&#39;: 0.0, &#39;whiten&#39;: False}, &#39;logistic__1&#39;: {&#39;C&#39;: 0.046415888336127774, &#39;class_weight&#39;: None, &#39;dual&#39;: False, &#39;fit_intercept&#39;: True, &#39;intercept_scaling&#39;: 1, &#39;l1_ratio&#39;: None, &#39;max_iter&#39;: 10000, &#39;multi_class&#39;: &#39;auto&#39;, &#39;n_jobs&#39;: None, &#39;penalty&#39;: &#39;l2&#39;, &#39;random_state&#39;: None, &#39;solver&#39;: &#39;lbfgs&#39;, &#39;tol&#39;: 0.1, &#39;verbose&#39;: 0, &#39;warm_start&#39;: False}}</pre><p>The corresponding plots are similar to those from the sklearn GridSearchCV(), except that the n_components chosen is 64 for the best score for the Codeflare pipelines grid_search_cv().</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/424/1*JwPu1HvFitOwfESbHIGutw.png" /></figure><p>The Jupyter notebook of this example is available <a href="https://github.com/project-codeflare/codeflare/blob/main/notebooks/plot_digits_pipe.ipynb">here</a>. Please download it and try it out to understand how you might convert an sklearn example to one that uses Codeflare pipelines. And please let us know what you think.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=13b884c8336f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codeflare/tuning-hyper-parameters-with-codeflare-pipelines-13b884c8336f">Tuning hyper-parameters with CodeFlare Pipelines</a> was originally published in <a href="https://medium.com/codeflare">CodeFlare</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>