Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update openai completion #2142

Merged
merged 7 commits into from
Dec 5, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ abstract class CognitiveServicesBaseNoHandler(val uid: String) extends Transform
.setHandler(handlingFunc _)
.setConcurrency(getConcurrency)
.setConcurrentTimeout(get(concurrentTimeout))
.setTimeout(getTimeout)
.setErrorCol(getErrorCol),
new DropColumns().setCol(dynamicParamColName)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package com.microsoft.azure.synapse.ml.services.openai

import com.microsoft.azure.synapse.ml.codegen.GenerationUtils
import com.microsoft.azure.synapse.ml.services.{HasAPIVersion, HasServiceParams}
import com.microsoft.azure.synapse.ml.services.{CognitiveServicesBase, HasAPIVersion, HasServiceParams}
import com.microsoft.azure.synapse.ml.param.ServiceParam
import org.apache.spark.sql.Row
import spray.json.DefaultJsonProtocol._
Expand Down Expand Up @@ -244,3 +244,6 @@ trait HasOpenAITextParams extends HasOpenAISharedParams {
}
}

abstract class OpenAIServicesBase(override val uid: String) extends CognitiveServicesBase(uid: String) {
setDefault(timeout -> 360.0)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import scala.language.existentials

object OpenAIChatCompletion extends ComplexParamsReadable[OpenAIChatCompletion]

class OpenAIChatCompletion(override val uid: String) extends CognitiveServicesBase(uid)
class OpenAIChatCompletion(override val uid: String) extends OpenAIServicesBase(uid)
with HasOpenAITextParams with HasCognitiveServiceInput
with HasInternalJsonOutputParser with SynapseMLLogging {
logClass(FeatureNames.AiServices.OpenAI)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import scala.language.existentials

object OpenAICompletion extends ComplexParamsReadable[OpenAICompletion]

class OpenAICompletion(override val uid: String) extends CognitiveServicesBase(uid)
class OpenAICompletion(override val uid: String) extends OpenAIServicesBase(uid)
with HasOpenAITextParams with HasPromptInputs with HasCognitiveServiceInput
with HasInternalJsonOutputParser with SynapseMLLogging {
logClass(FeatureNames.AiServices.OpenAI)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import scala.language.existentials

object OpenAIEmbedding extends ComplexParamsReadable[OpenAIEmbedding]

class OpenAIEmbedding (override val uid: String) extends CognitiveServicesBase(uid)
class OpenAIEmbedding (override val uid: String) extends OpenAIServicesBase(uid)
with HasOpenAISharedParams with HasCognitiveServiceInput with SynapseMLLogging {
logClass(FeatureNames.AiServices.OpenAI)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ class OpenAIPrompt(override val uid: String) extends Transformer
postProcessingOptions -> Map.empty,
outputCol -> (this.uid + "_output"),
errorCol -> (this.uid + "_error"),
dropPrompt -> true
dropPrompt -> true,
timeout -> 360.0
)

override def setCustomServiceName(v: String): this.type = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ class OpenAIChatCompletionSuite extends TransformerFuzzing[OpenAIChatCompletion]
import spark.implicits._

lazy val completion: OpenAIChatCompletion = new OpenAIChatCompletion()
.setDeploymentName(deploymentName)
.setCustomServiceName(openAIServiceName)
.setMaxTokens(200)
.setDeploymentName(deploymentNameGpt4)
.setCustomServiceName(openAIServiceNameGpt4)
.setApiVersion("2023-05-15")
.setMaxTokens(5000)
.setOutputCol("out")
.setMessagesCol("messages")
.setSubscriptionKey(openAIAPIKey)
.setTemperature(0)
.setSubscriptionKey(openAIAPIKeyGpt4)


lazy val goodDf: DataFrame = Seq(
Expand Down Expand Up @@ -48,8 +50,97 @@ class OpenAIChatCompletionSuite extends TransformerFuzzing[OpenAIChatCompletion]
null //scalastyle:ignore null
).toDF("messages")

lazy val slowDf: DataFrame = Seq(
Seq(
OpenAIMessage("system", "You help summarize content"),
OpenAIMessage("user",
"""
Given the following list of Article and their descriptions:
Article ID: 13d98f40-517a-4c95-bd84-27df6ea6a2a1
Article Description: Pride and Prejudice
Article ID: 6a8a8c95-ef38-4e7b-99b4-81756f602c36
Article Description: Romeo and Juliet
Article ID: 4b7b3c82-6c3e-45cf-9f6b-982a45f1dbad
Article Description: Calculus Made Easy
Article ID: 8f9d1f15-1aef-4a35-8301-ec8aa869f7b3
Article Description: Moby Dick; Or, The Whale
Article ID: 2d8a6e74-7b58-4a63-bc8d-3f5e90ab5b19
Article Description: The Scarlet Letter
Article ID: f8c0cfea-42e6-49e2-9f67-dc5ea20197c5
Article Description: A Christmas Carol in Prose
Article ID: 1a743f5b-98b3-43b5-b27e-9f631e7c625e
Article Description: Alice's Adventures in Wonderland
Article ID: 649da1a1-4df4-41e8-b4c2-943d8d1c47b7
Article Description: The Eyes Have It
Article ID: d35b7e0b-6fc5-4bf5-9d27-96d4894950b9
Article Description: Dracula
Article ID: a32faffd-d3f3-4f79-b9a6-47c1f6f8e299
Article Description: The Great Gatsby
Article ID: 9ea09862-0e44-4a58-81c1-c3449c7644c0
Article Description: A Doll's House : a play
Article ID: 3a6f64b8-3aa4-4f6e-bc6c-7df16ff0ce28
Article Description: The Picture of Dorian Gray
Article ID: 815b62c6-4c5f-43a1-844c-65d73a32162a
Article Description: A Modest Proposal
Article ID: d8e51b1a-1ea8-4a71-8ea3-ff4a2b2a00cf
Article Description: The Importance of Being Earnest: A Trivial Comedy for Serious People
Article ID: 682cfd7c-4a6e-4c87-88ae-9a2e7a61bf6b
Article Description: Metamorphosis
Article ID: 9a26b5c7-7438-45f3-b9da-8fc7ef3dd84c
Article Description: The Complete Works of William Shakespeare
Article ID: 6b2e7e26-2540-4a3a-810c-7d72fc4ec2cf
Article Description: The Strange Case of Dr. Jekyll and Mr. Hyde
Article ID: f7112843-719d-4a20-81a2-8767d7a18f15
Article Description: Middlemarch
Article ID: b25e40b0-5634-48da-b4fc-15dbbb3a20db
Article Description: A Room with a View
Article ID: a13b5f45-8cf2-4f16-8edf-751b3b4b29b8
Article Description: A Tale of Two Cities
Article ID: c8c34f96-b2c4-4dab-8d6d-b042e8c9031f
Article Description: The Yellow Wallpaper
Article ID: 45d8d855-e129-4c10-8690-34d5fbcff2d9
Article Description: Little Women; Or, Meg, Jo, Beth, and Amy
Article ID: 9a887890-1c48-4be0-b796-c542bfb7f3db
Article Description: The Adventures of Sherlock Holmes
Article ID: f26c78da-1e57-4d5e-a7dd-42e64d90f804
Article Description: Jane Eyre: An Autobiography
Article ID: b1d14760-1c6d-4b91-b58c-48a907f891b3
Article Description: Great Expectations
Article ID: 57396190-8a5d-4a2b-90ab-9c3bc02e7fb6
Article Description: The Enchanted April
Article ID: 704132cb-0bf4-46de-8b04-2d8c2ad4a409
Article Description: Adventures of Huckleberry Finn
Article ID: bcf666b2-803b-45a2-a36b-38a8f5641e20
Article Description: The Blue Castle: a novel
Article ID: 0c13bf68-7d10-4a02-8d1a-24290b623db4
Article Description: The Prince
Article ID: e13d8a92-7152-4c0c-8e45-1ff6485e3f92
Article Description: Cranford
Classify the articles into one of the article classes listed below:
Fantasy, Historical Fiction, Thriller, Romance, Science Fiction,
Mystery, Poetry, Drama, Classics, Humor, Religion, Philosophy, Psychology, Business, Other
For each article, please ensure that you do the following:
Ignore the numbers in the description.
Provide a rating between 0 and 10 of how confident you are with the classification
as well as a short justification.
Please provide your response in pure JSON syntax for all articles in the list as shown below
[
{"article_id": value,
"article_name": value,
"article_class": value,
"article_description": value,
"confidence": value,
"justification": value
}
]
Please do not include any other contextual words
other than proper JSON object for each article.""".stripMargin)
)
).toDF("messages")

test("Basic Usage") {
testCompletion(completion, goodDf)
testCompletion(completion, slowDf)
}

test("Robustness to bad inputs") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ trait OpenAIAPIKey {
lazy val openAIServiceName: String = "synapseml-openai"
lazy val deploymentName: String = "gpt-35-turbo"
lazy val modelName: String = "gpt-35-turbo"
lazy val openAIAPIKeyGpt4: String = sys.env.getOrElse("OPENAI_API_KEY_2", Secrets.OpenAIApiKeyGpt4)
lazy val openAIServiceNameGpt4: String = "synapseml-openai-2"
lazy val deploymentNameGpt4: String = "gpt-4"
lazy val modelNameGpt4: String = "gpt-4"
}

class OpenAICompletionSuite extends TransformerFuzzing[OpenAICompletion] with OpenAIAPIKey with Flaky {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ trait ConcurrencyParams extends Wrappable {
case Some(v) => setConcurrentTimeout(v)
case None => clear(concurrentTimeout)
}

setDefault(concurrency -> 1, timeout -> 60.0)

}

trait HasURL extends Params {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class SimpleHTTPTransformer(val uid: String)
.setHandler(getHandler)
.setConcurrency(getConcurrency)
.setConcurrentTimeout(get(concurrentTimeout))
.setTimeout(getTimeout)
.setInputCol(parsedInputCol)
.setOutputCol(unparsedOutputCol))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ object Secrets {

lazy val CognitiveApiKey: String = getSecret("cognitive-api-key")
lazy val OpenAIApiKey: String = getSecret("openai-api-key")
lazy val OpenAIApiKeyGpt4: String = getSecret("openai-api-key-2")

lazy val CustomSpeechApiKey: String = getSecret("custom-speech-api-key")
lazy val ConversationTranscriptionUrl: String = getSecret("conversation-transcription-url")
Expand Down
Loading