自学内容网 自学内容网

OPC UA遇见chatGPT

     

         最近opc 基金会将召开一个会议,主题是”OPC UA meets IT“。由此可见,工业自动化行业也开始研究和评估chatGPT带来的影响了。

本文谈谈本人对OPC UA 与chatGPT结合的初步实验和思考。

构建OPC UA 信息模型

         chatGPT 的确非常强大了,使用自然语言描述一个设备的信息模型,它能够直接生成该设备的OPC UA 信息模型,并且以OPCUA nodeset2.xml 格式输出:

代码


from openai import OpenAI

client = OpenAI(
    api_key="sk-xxxxxx",
    base_url="https://api.chatanywhere.tech/v1"
)
 
completion = client.chat.completions.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "system", "content": "creat opc ua information model ,output modeset2 xml format"},
    {"role": "user", "content": "typename motor ,attributes include current,voltage temperature,power data type float"}
  ],
  temperature=0.3,
)
 
print(completion.choices[0].message.content)

结果

runfile('E:/yao2024/python2024/chatgptSimple.py', wdir='E:/yao2024/python2024')
```xml
<?xml version="1.0" encoding="UTF-8"?>
<opc:Model xmlns:opc="http://opcfoundation.org/UA/2011/03/UANodeSet.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <opc:ObjectType NodeId="ns=1;i=1001" BrowseName="MotorType">
        <opc:DisplayName>Motor</opc:DisplayName>
        <opc:References>
            <opc:Reference ReferenceType="HasProperty" IsForward="false">i=47</opc:Reference>
            <opc:Reference ReferenceType="HasProperty" IsForward="false">i=48</opc:Reference>
            <opc:Reference ReferenceType="HasProperty" IsForward="false">i=49</opc:Reference>
            <opc:Reference ReferenceType="HasProperty" IsForward="false">i=50</opc:Reference>
        </opc:References>
    </opc:ObjectType>
    <opc:Variable NodeId="ns=1;i=47" BrowseName="Current" DataType="Float">
        <opc:DisplayName>Current</opc:DisplayName>
    </opc:Variable>
    <opc:Variable NodeId="ns=1;i=48" BrowseName="Voltage" DataType="Float">
        <opc:DisplayName>Voltage</opc:DisplayName>
    </opc:Variable>
    <opc:Variable NodeId="ns=1;i=49" BrowseName="Temperature" DataType="Float">
        <opc:DisplayName>Temperature</opc:DisplayName>
    </opc:Variable>
    <opc:Variable NodeId="ns=1;i=50" BrowseName="Power" DataType="Float">
        <opc:DisplayName>Power</opc:DisplayName>
    </opc:Variable>
</opc:Model>

         酷吧?关键是提示要写好。如何生成特定行业的DSL 语言,还需要进一步研究。例如生产线的工艺流程编排。这将极大提升系统设计和维护的工作效率,降低了工程成本。

生成结构化数据

  下面的例子演示如何利用chatGPT按照JSON 模板,构建结构化数据。

程序

import json
import os
from langchain_openai import ChatOpenAI
from langchain.agents import initialize_agent, Tool
from langchain.agents.mrkl import prompt
os.environ['OPENAI_API_KEY'] ="sk-xxxxxxxx"
os.environ['OPENAI_BASE_URL'] ="https://api.chatanywhere.tech/v1"
def get_template(productClass):
    #print(productClass)
 
    answer = [
        {"type": "product type",
         "brand": "product brand",
         "manufacture":"product manufacture",
         "color":"color of prodcts",
         "size":"product size"}
         
    ]
  
    return json.dumps(answer)
def device_control(device_id):
    print(device_id)
    status=True
    answer = [
        {"状态": status}
    ]
    return json.dumps(answer)
def lang_chain_agent(text):
    llm = ChatOpenAI(model_name="gpt-3.5-turbo",base_url="https://api.chatanywhere.tech/v1")

    tools = [
        Tool(
            name = "get_template",
            func=get_template,
            description="use this tool when you need to get product model tempplate ,To use the tool, you must provide  chinese product class",
        )
 
    ]

    agent = initialize_agent(
        tools,
        llm,
        agent="zero-shot-react-description",
        agent_kwargs=dict(suffix='Answer should be  json. ' + prompt.SUFFIX), 
        verbose=True,
        return_intermediate_steps=True)

    response = agent({"input": text})

    return response
lang_chain_agent("根据如下数据生成符合模型样板的json 产品数据,  类型 足球 品牌 小少年 制造商 鹰派运动用品公司 颜色 红色 尺寸 12 英寸")

结果

> Entering new AgentExecutor chain...
I need to use the get_template tool to generate the product model template for a football product.
Action: get_template
Action Input: 足球
Observation: [{"type": "product type", "brand": "product brand", "manufacture": "product manufacture", "color": "color of prodcts", "size": "product size"}]
Thought:Now I can fill in the template with the provided data.
Final Answer: {"type": "足球", "brand": "小少年", "manufacture": "鹰派运动用品公司", "color": "红色", "size": "12 英寸"}

> Finished chain.

chatGPT 访问OPC UA 服务器

    OPC UA是自动化行业广泛应用的工业标准,我们设想可以在chatGPT Agent 中增加一个OPCUA Client ,用它来获取现场设备的状态,并且实现chatGPT对物理设备的控制。其架构如下:


原文地址:https://blog.csdn.net/yaojiawan/article/details/137472767

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!