From 763bc9a9dd3c2cef3c228d3efa5be121b65bec29 Mon Sep 17 00:00:00 2001 From: Joschka Braun Date: Thu, 1 Feb 2024 18:08:42 -0500 Subject: [PATCH] feat: add json mode examples to cookbook --- .../tracing_with_openai_endpoint_directly.ts | 13 +++++++++++++ src/cookbook/tracing_without_deployed_prompt.ts | 17 +++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/cookbook/tracing_with_openai_endpoint_directly.ts b/src/cookbook/tracing_with_openai_endpoint_directly.ts index b560a70..980fe7f 100644 --- a/src/cookbook/tracing_with_openai_endpoint_directly.ts +++ b/src/cookbook/tracing_with_openai_endpoint_directly.ts @@ -128,6 +128,19 @@ async function main3() { return result; } +async function main4() { + const response = await openai.chat.completions.create({ + model: 'gpt-4-1106-preview', + messages: [ + { role: 'system', content: 'You are a helpful assistant talking in JSON.' }, + { role: 'user', content: 'What are you?' }, + ], + response_format: { type: 'json_object' }, + }); + return response.choices[0].message.content ?? ''; +} + main().then((result) => console.log(result)); main2().then((result) => console.log(result)); main3().then((result) => console.log(result)); +main4().then((r) => console.log(r)); diff --git a/src/cookbook/tracing_without_deployed_prompt.ts b/src/cookbook/tracing_without_deployed_prompt.ts index b75d534..0becd38 100644 --- a/src/cookbook/tracing_without_deployed_prompt.ts +++ b/src/cookbook/tracing_without_deployed_prompt.ts @@ -123,5 +123,22 @@ async function main2() { return result; } +async function main3() { + const completion: Completion = { + llm_configuration: { + model: 'gpt-4-1106-preview', + provider: 'openai', + model_params: { temp: 0.0, response_format: { type: 'json_object' } }, + messages: [ + { role: 'system', content: 'You are a helpful assistant talking in JSON.' }, + { role: 'user', content: 'What are you?' }, + ], + }, + metadata: { source: 'parea-js-sdk' }, + }; + return await p.completion(completion); +} + main().then((result) => console.log(result)); main2().then((result) => console.log(result)); +main3().then((result) => console.log(result));