-
Notifications
You must be signed in to change notification settings - Fork 847
feat: [DRAFT - DO NOT MERGE] df.ai.gen - allow PySpark users to easily leverage OpenAI prompting #2267
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
Open
sss04
wants to merge
10
commits into
microsoft:master
Choose a base branch
from
sss04:shyamsai/promptSugar
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: [DRAFT - DO NOT MERGE] df.ai.gen - allow PySpark users to easily leverage OpenAI prompting #2267
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b6740c9
Initial Commits DataFrame AI Extensions
fbee075
Add AI to dataframe extension
316df3c
changes for testing - setting URL
eebd5c2
Undo URL Changes
54bf17e
.gen initial commit - tested and works
sss04 2112875
Prompt Sugar around .gen with hacky authentication
sss04 548ec3d
Merge branch 'master' into shyamsai/promptSugar
sss04 c8494dd
getting rid of deprecated and internal function usage
sss04 a0777a5
Merge branch 'shyamsai/promptSugar' of https://github.com/sss04/Synap…
sss04 0f635a7
Remove print statement
sss04 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
cognitive/src/main/python/synapse/ml/services/openai/DataFrameAIExtensions.py
This file contains hidden or 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,52 @@ | ||
# Copyright (C) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See LICENSE in project root for information. | ||
|
||
import sys | ||
import os, json, subprocess, unittest | ||
|
||
if sys.version >= "3": | ||
basestring = str | ||
|
||
import pyspark | ||
from pyspark import SparkContext | ||
from pyspark import sql | ||
from pyspark.ml.param.shared import * | ||
from pyspark.rdd import RDD | ||
|
||
from pyspark.sql import SparkSession, SQLContext | ||
|
||
from synapse.ml.core.init_spark import * | ||
spark = init_spark() | ||
sc = SQLContext(spark.sparkContext) | ||
|
||
class AIFunctions: | ||
def __init__(self, df): | ||
self.df = df | ||
self.subscriptionKey = None | ||
self.deploymentName = None | ||
self.customServiceName = None | ||
|
||
def setup(self, subscriptionKey = None, deploymentName = None, customServiceName = None): | ||
self.subscriptionKey = subscriptionKey | ||
self.deploymentName = deploymentName | ||
self.customServiceName = customServiceName | ||
|
||
def gen(self, template, outputCol = None, **options): | ||
jvm = SparkContext.getOrCreate()._jvm | ||
prompt = jvm.com.microsoft.azure.synapse.ml.services.openai.OpenAIPrompt() | ||
prompt = prompt.setSubscriptionKey(self.subscriptionKey) | ||
prompt = prompt.setDeploymentName(self.deploymentName) | ||
prompt = prompt.setCustomServiceName(self.customServiceName) | ||
prompt = prompt.setOutputCol(outputCol) | ||
prompt = prompt.setPromptTemplate(template) | ||
results = prompt.transform(self.df._jdf) | ||
results.createOrReplaceTempView("my_temp_view") | ||
results = spark.sql("SELECT * FROM my_temp_view") | ||
return results | ||
|
||
def get_AI_functions(df): | ||
if not hasattr(df, "_ai_instance"): | ||
df._ai_instance = AIFunctions(df) | ||
return df._ai_instance | ||
|
||
setattr(pyspark.sql.DataFrame, "ai", property(get_AI_functions)) |
1 change: 1 addition & 0 deletions
1
cognitive/src/main/python/synapse/ml/services/openai/__init__.py
This file contains hidden or 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 @@ | ||
from DataFrameAIExtensions import * |
This file contains hidden or 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
73 changes: 73 additions & 0 deletions
73
cognitive/src/test/python/synapsemltest/services/openai/test_DataFrameAIExtentions.py
This file contains hidden or 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,73 @@ | ||
# Copyright (C) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See LICENSE in project root for information. | ||
|
||
# Prepare training and test data. | ||
import unittest | ||
import os, json, subprocess, unittest | ||
|
||
from synapse.ml.io.http import * | ||
from pyspark.sql.types import * | ||
from synapse.ml.services.openai import * | ||
|
||
from pyspark.sql import SparkSession, SQLContext | ||
|
||
from synapse.ml.core.init_spark import * | ||
spark = init_spark() | ||
sc = SQLContext(spark.sparkContext) | ||
|
||
class DataFrameAIExtentionsTest(unittest.TestCase): | ||
def test_gen(self): | ||
schema = StructType([ | ||
StructField("text", StringType(), True), | ||
StructField("category", StringType(), True) | ||
]) | ||
|
||
data = [ | ||
("apple", "fruits"), | ||
("mercedes", "cars"), | ||
("cake", "dishes"), | ||
] | ||
|
||
df = spark.createDataFrame(data, schema) | ||
|
||
secretJson = subprocess.check_output( | ||
"az keyvault secret show --vault-name mmlspark-build-keys --name openai-api-key-2", | ||
shell=True, | ||
) | ||
openai_api_key = json.loads(secretJson)["value"] | ||
print(openai_api_key) | ||
|
||
df.ai.setup(subscriptionKey=openai_api_key, deploymentName="gpt-35-turbo", customServiceName="synapseml-openai-2") | ||
|
||
results = df.ai.gen("Complete this comma separated list of 5 {category}: {text}, ", outputCol="outParsed") | ||
results.select("outParsed").show(truncate = False) | ||
nonNullCount = results.filter(col("outParsed").isNotNull()).count() | ||
assert (nonNullCount == 3) | ||
|
||
def test_gen_2(self): | ||
schema = StructType([ | ||
StructField("name", StringType(), True), | ||
StructField("address", StringType(), True) | ||
]) | ||
|
||
data = [ | ||
("Anne F.", "123 First Street, 98053"), | ||
("George K.", "345 Washington Avenue, London"), | ||
] | ||
|
||
df = spark.createDataFrame(data, schema) | ||
|
||
secretJson = subprocess.check_output( | ||
"az keyvault secret show --vault-name mmlspark-build-keys --name openai-api-key-2", | ||
shell=True, | ||
) | ||
openai_api_key = json.loads(secretJson)["value"] | ||
|
||
df.ai.setup(subscriptionKey=openai_api_key, deploymentName="gpt-35-turbo", customServiceName="synapseml-openai-2") | ||
results = df.ai.gen("Generate the likely country of {name}, given that they are from {address}. It is imperitive that your response contains the country only, no elaborations.", outputCol="outParsed") | ||
results.select("outParsed").show(truncate = False) | ||
nonNullCount = results.filter(col("outParsed").isNotNull()).count() | ||
assert (nonNullCount == 2) | ||
|
||
if __name__ == "__main__": | ||
result = unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.