-
Notifications
You must be signed in to change notification settings - Fork 834
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add ChatGPT through the
OpenAIChatCompletion
transformer (#1887)
- Loading branch information
1 parent
7657089
commit 9f634a6
Showing
16 changed files
with
299 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...src/main/scala/com/microsoft/azure/synapse/ml/cognitive/openai/OpenAIChatCompletion.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright (C) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in project root for information. | ||
|
||
package com.microsoft.azure.synapse.ml.cognitive.openai | ||
|
||
import com.microsoft.azure.synapse.ml.cognitive.{ | ||
CognitiveServicesBase, HasCognitiveServiceInput, HasInternalJsonOutputParser | ||
} | ||
import com.microsoft.azure.synapse.ml.logging.SynapseMLLogging | ||
import com.microsoft.azure.synapse.ml.param.AnyJsonFormat.anyFormat | ||
import org.apache.http.entity.{AbstractHttpEntity, ContentType, StringEntity} | ||
import org.apache.spark.ml.ComplexParamsReadable | ||
import org.apache.spark.ml.param.Param | ||
import org.apache.spark.ml.util._ | ||
import org.apache.spark.sql.Row | ||
import org.apache.spark.sql.types._ | ||
import spray.json._ | ||
import spray.json.DefaultJsonProtocol._ | ||
|
||
import scala.language.existentials | ||
|
||
object OpenAIChatCompletion extends ComplexParamsReadable[OpenAIChatCompletion] | ||
|
||
class OpenAIChatCompletion(override val uid: String) extends CognitiveServicesBase(uid) | ||
with HasOpenAITextParams with HasCognitiveServiceInput | ||
with HasInternalJsonOutputParser with SynapseMLLogging { | ||
logClass() | ||
|
||
val messagesCol: Param[String] = new Param[String]( | ||
this, "messagesCol", "The column messages to generate chat completions for," + | ||
" in the chat format. This column should have type Array(Struct(role: String, content: String)).") | ||
|
||
def getMessagesCol: String = $(messagesCol) | ||
|
||
def setMessagesCol(v: String): this.type = set(messagesCol, v) | ||
|
||
def this() = this(Identifiable.randomUID("OpenAIChatCompletion")) | ||
|
||
def urlPath: String = "" | ||
|
||
override private[ml] def internalServiceType: String = "openai" | ||
|
||
override def setCustomServiceName(v: String): this.type = { | ||
setUrl(s"https://$v.openai.azure.com/" + urlPath.stripPrefix("/")) | ||
} | ||
|
||
override protected def prepareUrlRoot: Row => String = { row => | ||
s"${getUrl}openai/deployments/${getValue(row, deploymentName)}/chat/completions" | ||
} | ||
|
||
override protected def prepareEntity: Row => Option[AbstractHttpEntity] = { | ||
r => | ||
lazy val optionalParams: Map[String, Any] = getOptionalParams(r) | ||
val messages = r.getAs[Seq[Row]](getMessagesCol) | ||
Some(getStringEntity(messages, optionalParams)) | ||
} | ||
|
||
override val subscriptionKeyHeaderName: String = "api-key" | ||
|
||
override def shouldSkip(row: Row): Boolean = | ||
super.shouldSkip(row) || Option(row.getAs[Row](getMessagesCol)).isEmpty | ||
|
||
override protected def getVectorParamMap: Map[String, String] = super.getVectorParamMap | ||
.updated("messages", getMessagesCol) | ||
|
||
override def responseDataType: DataType = ChatCompletionResponse.schema | ||
|
||
private[this] def getStringEntity(messages: Seq[Row], optionalParams: Map[String, Any]): StringEntity = { | ||
val mappedMessages: Seq[Map[String, String]] = messages.map { m => | ||
Seq("role", "content", "name").map(n => | ||
n -> Option(m.getAs[String](n)) | ||
).toMap.filter(_._2.isDefined).mapValues(_.get) | ||
} | ||
val fullPayload = optionalParams.updated("messages", mappedMessages) | ||
new StringEntity(fullPayload.toJson.compactPrint, ContentType.APPLICATION_JSON) | ||
} | ||
|
||
} | ||
|
||
|
||
|
Oops, something went wrong.