Skip to content

Commit

Permalink
[KYUUBI #5828] [CHAT] Kyuubi chat engine supports ernie bot(文心一言)
Browse files Browse the repository at this point in the history
# 🔍 Description
## Issue References 🔗

This pull request fixes #5386

## Describe Your Solution 🔧

add a new backend(ernie bot) for the Chat engine.
<img width="1672" alt="Screenshot 2023-12-07 at 16 20 56" src="https://github.com/apache/kyuubi/assets/32693629/9850916c-3c4a-433c-8278-3068e7b37314">

## Types of changes 🔖

- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Test Plan 🧪

#### Behavior Without This Pull Request ⚰️

#### Behavior With This Pull Request 🎉

#### Related Unit Tests

---

# Checklists
## 📝 Author Self Checklist

- [ ] My code follows the [style guidelines](https://kyuubi.readthedocs.io/en/master/contributing/code/style.html) of this project
- [ ] I have performed a self-review
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] This patch was not authored or co-authored using [Generative Tooling](https://www.apache.org/legal/generative-tooling.html)

## 📝 Committer Pre-Merge Checklist

- [x] Pull request title is okay.
- [x] No license issues.
- [x] Milestone correctly set?
- [ ] Test coverage is ok
- [x] Assignees are selected.
- [x] Minimum number of approvals
- [x] No changes are requested

**Be nice. Be informative.**

Closes #5828 from zhaohehuhu/dev-1207.

Closes #5828

c7314d4 [Cheng Pan] Update externals/kyuubi-chat-engine/src/main/scala/org/apache/kyuubi/engine/chat/ernie/service/ErnieBotService.scala
0c4e010 [hezhao2] update doc
78e51b3 [hezhao2] add ernie into doc
2f4a638 [hezhao2] refactor
832bc04 [hezhao2] delete enum model
67214c5 [hezhao2] get rid of some java code
5677726 [hezhao2] java bean to scale case class
4d5c594 [hezhao2] refactor some params
7c44eb8 [hezhao2] refactor some params
56b9ad1 [hezhao2] refactor
a8e3d6c [hezhao2] refactor
7376d80 [hezhao2] Kyuubi chat engine supports ernie bot
4b72a09 [hezhao2] Kyuubi chat engine supports ernie bot

Lead-authored-by: hezhao2 <[email protected]>
Co-authored-by: Cheng Pan <[email protected]>
Signed-off-by: Cheng Pan <[email protected]>
  • Loading branch information
zhaohehuhu and pan3793 committed Dec 11, 2023
1 parent f9d27de commit 8ab4763
Show file tree
Hide file tree
Showing 19 changed files with 638 additions and 1 deletion.
7 changes: 6 additions & 1 deletion docs/configuration/settings.md

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions externals/kyuubi-chat-engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-jackson</artifactId>
<version>${retrofit.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.kyuubi.engine.chat.ernie.enums;

public enum ChatMessageRole {
FUNCTION("function"),

USER("user"),

ASSISTANT("assistant");

private final String value;

private ChatMessageRole(String value) {
this.value = value;
}

public String value() {
return this.value;
}

@Override
public String toString() {
return "ChatMessageRole{" + "value='" + value + '\'' + '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.kyuubi.engine.chat.api

class ApiHttpException(statusCode: Int, message: String, exception: Exception)
extends Exception(message, exception) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.kyuubi.engine.chat.api

import io.reactivex.Single
import retrofit2.http.{Body, Path, POST, Query}

import org.apache.kyuubi.engine.chat.ernie.bean.{ChatCompletionRequest, ChatCompletionResult}

trait ErnieBotApi {
@POST("/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/{model}")
def createChatCompletion(
@Path("model") model: String,
@Query("access_token") accessToken: String,
@Body chatCompletionRequest: ChatCompletionRequest): Single[ChatCompletionResult]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.kyuubi.engine.chat.ernie.bean

import java.lang.{Double => JDouble}
import java.util.{List => JList}

import com.fasterxml.jackson.annotation.JsonProperty

case class ChatCompletionRequest(
@JsonProperty("messages") messages: JList[ChatMessage],
@JsonProperty("functions") functions: JList[Function] = null,
@JsonProperty("temperature") temperature: JDouble = null,
@JsonProperty("top_p") topP: JDouble = null,
@JsonProperty("penalty_score") presenceScore: JDouble = null,
@JsonProperty("stream") stream: Boolean = false,
@JsonProperty("system") system: String = null,
@JsonProperty("stop") stop: JList[String] = null,
@JsonProperty("disable_search") disableSearch: Boolean = false,
@JsonProperty("enable_citation") enableCitation: Boolean = false,
@JsonProperty("user_id") userId: String = null)
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.kyuubi.engine.chat.ernie.bean

import java.lang.{Long => JLong}

import com.fasterxml.jackson.annotation.JsonProperty

case class ChatCompletionResult(
@JsonProperty("id") id: String,
@JsonProperty("object") obj: String,
@JsonProperty("created") created: JLong,
@JsonProperty("sentence_id") sentenceId: JLong,
@JsonProperty("is_end") isEnd: Boolean,
@JsonProperty("is_truncated") isTruncated: Boolean,
@JsonProperty("finish_reason") finishReason: String,
@JsonProperty("search_info") searchInfo: SearchInfo,
@JsonProperty("result") result: String,
@JsonProperty("need_clear_history") needClearHistory: Boolean,
@JsonProperty("ban_round") banRound: JLong,
@JsonProperty("usage") usage: Usage,
@JsonProperty("function_call") functionCall: FunctionCall,
@JsonProperty("error_msg") errorMsg: String,
@JsonProperty("error_code") errorCode: JLong)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.kyuubi.engine.chat.ernie.bean

import com.fasterxml.jackson.annotation.JsonProperty

case class ChatMessage(
@JsonProperty("role") role: String,
@JsonProperty("content") content: String,
@JsonProperty("name") name: String,
@JsonProperty("function_call") functionCall: FunctionCall = null)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.kyuubi.engine.chat.ernie.bean

import com.fasterxml.jackson.annotation.JsonProperty

case class Example(
@JsonProperty("role") role: String,
@JsonProperty("name") name: String,
@JsonProperty("content") content: String = null,
@JsonProperty("function_call") functionCall: FunctionCall = null)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.kyuubi.engine.chat.ernie.bean

import java.util.{List => JList}

import com.fasterxml.jackson.annotation.JsonProperty

case class Function(
@JsonProperty("name") name: String,
@JsonProperty("description") description: String,
@JsonProperty("parameters") parameters: Object,
@JsonProperty("responses") responses: Object = null,
@JsonProperty("examples") examples: JList[Example] = null)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.kyuubi.engine.chat.ernie.bean

import com.fasterxml.jackson.annotation.JsonProperty

case class FunctionCall(
@JsonProperty("name") name: String,
@JsonProperty("thoughts") thoughts: String,
@JsonProperty("arguments") arguments: String = null)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.kyuubi.engine.chat.ernie.bean

import java.lang.{Long => JLong}

import com.fasterxml.jackson.annotation.JsonProperty

case class PluginUsage(
@JsonProperty("name") name: String,
@JsonProperty("parse_tokens") parseTokens: JLong,
@JsonProperty("abstract_tokens") abstractTokens: JLong,
@JsonProperty("search_tokens") searchTokens: JLong,
@JsonProperty("total_tokens") totalTokens: JLong)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.kyuubi.engine.chat.ernie.bean

import java.lang.{Long => JLong}
import java.util.{List => JList}

import com.fasterxml.jackson.annotation.JsonProperty

case class SearchInfo(
@JsonProperty("is_beset") isBeset: JLong,
@JsonProperty("rewrite_query") rewriteQuery: String,
@JsonProperty("search_results") searchResults: JList[SearchResult])
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.kyuubi.engine.chat.ernie.bean

import com.fasterxml.jackson.annotation.JsonProperty

case class SearchResult(
@JsonProperty("index") index: java.lang.Long,
@JsonProperty("url") url: String,
@JsonProperty("title") title: String,
@JsonProperty("datasource_id") datasourceId: String)
Loading

0 comments on commit 8ab4763

Please sign in to comment.