Hi @VincentF, if you would like to enforce a json format from ChatGPT response in code repositories for example, you can provide an additional argument to your CompletionRequest “response_format”.
For Python Transforms, GptChatWithVisionCompletionRequest supports response format, and the Json enum is “JSON_OBJECT”. Please find an example code snippet below.
# NOTE: Libraries must be manually imported through "Libraries" in the left-hand panel
from transforms.api import transform, Output
from palantir_models.transforms import OpenAiGptChatWithVisionLanguageModelInput
from palantir_models.models import OpenAiGptChatWithVisionLanguageModel
from language_model_service_api.languagemodelservice_api_completion_v3 import (
GptChatWithVisionCompletionRequest,
GptChatCompletionResponse
)
from language_model_service_api.languagemodelservice_api import (
ChatMessageRole,
ChatMessageContent,
MultiContentChatMessage,
)
from pyspark.sql import functions as F
from pyspark.sql import types as T
MOCK = [
(1, "Great product, highly recommend!", 5),
(2, "Not bad, but could be better.", 3),
(3, "Terrible experience, will not buy again.", 1),
(4, "Okay product for the price.", 3),
(5, "Loved it, exactly what I needed!", 5)
]
@transform(
model=OpenAiGptChatWithVisionLanguageModelInput("ri.language-model-service..language-model.gpt-4-o"),
output=Output(""),
)
def compute_sentiment(ctx, model: OpenAiGptChatWithVisionLanguageModel, output):
def get_llm_response(prompt):
request: GptChatWithVisionCompletionRequest = GptChatWithVisionCompletionRequest(
[
MultiContentChatMessage(
contents=[
ChatMessageContent(text="Take the following review determine the sentiment of the review. Respond in json format {'llm_response': response to user question, 'certainty': how certain you are the response is accurate}"),
],
role=ChatMessageRole.SYSTEM,
),
MultiContentChatMessage(
contents=[
ChatMessageContent(text=prompt),
],
role=ChatMessageRole.USER,
)
],
max_tokens=200, temperature=0.8, response_format={"type": "JSON_OBJECT"})
response: GptChatCompletionResponse = model.create_chat_completion(request)
return response.choices[0].message.content
reviews_df = ctx.spark_session.createDataFrame(MOCK, schema=["review_id", "review_text", "rating"])
get_llm_response_udf = F.udf(get_llm_response, T.StringType())
# Define schema for JSON output
json_schema = T.StructType([
T.StructField("llm_response", T.StringType(), True),
T.StructField("certainty", T.StringType(), True)
])
output_df = reviews_df.withColumn('llm_response', get_llm_response_udf(F.col('review_text')))
output_df = output_df.withColumn("json_parsed", F.from_json(F.col("llm_response"), json_schema))
output_df = output_df.select(
F.col("review_id"),
F.col("review_text"),
F.col("json_parsed.llm_response").alias("llm_response_json"),
F.col("json_parsed.certainty").alias("certainty")
)
return output.write_dataframe(output_df)
Similarly, if you would like to enforce Json format in TS Functions you can similarly pass a responseFormat: {type: “json_object”} to createChatCompletion. Example code below:
import { Function } from "@foundry/functions-api"
// NOTE: Model Imports must be manually added through "Resource Imports" in the left-hand panel
import { GPT_4o } from "@foundry/models-api/language-models"
export class MyFunctions {
@Function()
public async createChatCompletion(userInput: string): Promise<string | undefined> {
const response = await GPT_4o.createChatCompletion({
params: {
"temperature": 0,
"maxTokens": 1000,
responseFormat: {type: "json_object"}
},
messages: [{role:"SYSTEM", contents:[{text: "Provide response in json format specifying the answer and how certain you are of the response"}]},{ role: "USER", contents: [{ text: userInput }] }],
});
return response.choices[0].message.content;
}
}