Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions openai_fdddf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# OpenAI连接器
代码实现支持代理配置的OpenAI连接器

## 逻辑详情

### listModels

获取模型列表

入参: 无
出参: List<String>

### getChatCompletions

获取聊天 completions

入参: `List<ChatMessage>`, String modelId
出参: ChatResponse


### getChatCompletionsWithPrompt

获取聊天 completions

入参: String prompt, String modelId,
出参: ChatResponse


## 使用步骤说明

1. 应用集成中导入连接库
2. 添加连接器并配置参数
* secretKey String OpenAI密钥
* customEndpoint String 自定义endpoint,空填写为null
* proxyHost String 代理地址,空填写为null
* proxyPort Integer 代理端口,空填写为0
* proxyUsername String 代理用户名,空填写为null
* proxyPassword String 代理密码,空填写为null
* proxyType Integer 代理类型 0:http 1:socks
3. 逻辑调用示例截图

![img](Snipaste_2024-09-28_21-50-04.jpg)

## 应用演示链接

[使用了本依赖库的制品应用链接]

https://dev-testdify-qa.app.codewave.163.com/testopenai



Binary file added openai_fdddf/Snipaste_2024-09-28_21-50-04.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mvn install:install-file -Dfile="nasl-metadata-maven-plugin-1.3.0.jar" -DpomFile="pom.xml"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mvn install:install-file -Dfile="nasl-metadata-maven-plugin-1.3.0.jar" -DpomFile="pom.xml"
Binary file not shown.
74 changes: 74 additions & 0 deletions openai_fdddf/jar/nasl-metadata-maven-plugin-1.3.0/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.netease.lowcode</groupId>
<artifactId>nasl-metadata-maven-plugin</artifactId>
<version>1.3.0</version>
<packaging>maven-plugin</packaging>

<name>Nasl Metadata Maven Plugin</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0.10</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-manager</artifactId>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.0.8</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-common-artifact-filters</artifactId>
<version>3.0.1</version>
</dependency>

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-aether-provider</artifactId>
<version>3.3.9</version>
</dependency>

<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-api</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.2</version>
<configuration>
<goalPrefix>nasl-metadata-maven-plugin</goalPrefix>
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
</configuration>
</plugin>
</plugins>
</build>
</project>
78 changes: 78 additions & 0 deletions openai_fdddf/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.9.RELEASE</version><!--与当前制品应用默认版本统一-->
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.fdddf</groupId>
<artifactId>openai</artifactId>
<version>1.0.4</version>
<name>OpenAI连接器</name>
<description>支持配置代理的OpenAI连接器</description>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<nasl.ide.version>3.3</nasl.ide.version>
</properties>
<dependencies>
<!--本案例是本地系统引入nasl-metadata-collector-0.8.0.jar的方式。
若把nasl-metadata-collector-0.8.0.jar安装到自己的maven仓库,
注意修改artifactId和groupId的情况下,不要使用<scope>system</scope>,会在发布时造成依赖中断。
不修改artifactId和groupId的情况下,nasl-metadata-maven-plugin会做特殊处理-->
<dependency>
<artifactId>nasl-metadata-collector</artifactId>
<groupId>com.netease.lowcode</groupId>
<version>0.8.0</version>
<optional>true</optional>
<scope>system</scope>
<systemPath>${project.basedir}/jar/nasl-metadata-collector-0.8.0.jar</systemPath>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<!--制品应用使用Springboot框架,父应用引用了的包,为了防止版本冲突,scope可设置为provided-->
<dependency>
<groupId>com.unfbx</groupId>
<artifactId>chatgpt-java</artifactId>
<version>1.1.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.netease.lowcode</groupId>
<artifactId>nasl-metadata-maven-plugin</artifactId>
<version>1.3.0</version>
<configuration>
<jarWithDependencies>false</jarWithDependencies>
</configuration>
<executions>
<execution>
<goals>
<goal>archive</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
29 changes: 29 additions & 0 deletions openai_fdddf/src/main/java/com/fdddf/openai/ChatMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.fdddf.openai;

import com.netease.lowcode.core.annotation.NaslStructure;

@NaslStructure
public class ChatMessage {

/**
* 角色 user: 用户,system:系统,assistant:Assistant
*/
public String roleName;

public String content;

public ChatMessage() {}

public ChatMessage(String roleName, String content) {
this.content = content;
this.roleName = roleName;
}

@Override
public String toString() {
return "ChatMessage{" +
"roleName='" + roleName + '\'' +
", content='" + content + '\'' +
'}';
}
}
30 changes: 30 additions & 0 deletions openai_fdddf/src/main/java/com/fdddf/openai/ChatResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.fdddf.openai;

import com.netease.lowcode.core.annotation.NaslStructure;

import java.util.List;

@NaslStructure
public class ChatResponse {
public List<ChatMessage> messages;
public String model;
public Long promptTokens;
public Long completionTokens;
public Long totalTokens;

public Integer errorCode;
public String errorMessage;

@Override
public String toString() {
return "ChatResponse{" +
"messages=" + messages +
", model='" + model +
", promptTokens=" + promptTokens +
", completionTokens=" + completionTokens +
", totalTokens=" + totalTokens +
", errorCode=" + errorCode +
", errorMessage='" + errorMessage +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.fdddf.openai;

import lombok.SneakyThrows;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.sse.EventSourceListener;

import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.function.Function;

public class ConsoleEventSourceListener extends EventSourceListener {

public Function<String, Boolean> onMessageHandler;
public Function<String, Boolean> onFailureHandler;
public Function<String, Boolean> onCompleteHandler;

public CountDownLatch countDownLatch;

@Override
public void onOpen(okhttp3.sse.EventSource eventSource, Response response) {
System.out.println("EventSource opened!");
}

@Override
public void onClosed(okhttp3.sse.EventSource eventSource) {
System.out.println("EventSource closed!");
countDownLatch.countDown();
if (onCompleteHandler != null) {
onCompleteHandler.apply("");
}
}

@Override
public void onEvent(okhttp3.sse.EventSource eventSource, String id, String type, String data) {
// System.out.println("EventSource received: " + data);
if (data.equals("[DONE]")) {
countDownLatch.countDown();
return;
}
if (onMessageHandler != null) {
onMessageHandler.apply(data);
}
}

@SneakyThrows
@Override
public void onFailure(okhttp3.sse.EventSource eventSource, Throwable t, Response response) {
System.out.println("EventSource failed!");
if (onFailureHandler != null) {
onFailureHandler.apply(t.toString());
}
if (Objects.isNull(response)) {
countDownLatch.countDown();
return;
}
ResponseBody body = response.body();
if (Objects.nonNull(body)) {
System.out.println("Response none null: " + body.string());
countDownLatch.countDown();
return;
} else {
System.out.println("Response error: " + t.toString());
}
eventSource.cancel();
countDownLatch.countDown();
}
}
Loading