From cdb16d004a6506a40f742a3832ca0115e9ad4ec5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javis=20V=2E=20P=C3=A9rez?= Date: Wed, 27 Nov 2024 09:43:42 -0400 Subject: [PATCH] Update InputCodeSnippets.vue --- .../src/components/InputCodeSnippets.vue | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/frontend/dev-mode/src/components/InputCodeSnippets.vue b/frontend/dev-mode/src/components/InputCodeSnippets.vue index cf6780d1..3c698381 100644 --- a/frontend/dev-mode/src/components/InputCodeSnippets.vue +++ b/frontend/dev-mode/src/components/InputCodeSnippets.vue @@ -15,16 +15,18 @@ const parameters = inject>('parameters', {}) const pythonSnippet = computed(() => `import openai client = openai.OpenAI( - base_url="${apiUrl}/v1", # "http://:port" - api_key = "sk-no-key-required" + base_url="${apiUrl}/v1",${ + parameters.value.api_key ? '\n api_key="'+parameters.value.api_key+'"' : '' + } ) completion = client.chat.completions.create( model="${currentModel.value}", - messages=[${ + messages=[ + {"role": "system", "content": "${session.value.prompt}"},${ (session.value.transcript as Array).map(([role, [entry]]) => { if (role.toLowerCase() === '{{user}}') { - return `\n { role: "user", content: "${entry.content}" }` + return `\n { "role": "user", "content": "${entry.content}" }` } }) .filter(Boolean) @@ -47,14 +49,16 @@ for chunk in completion: const nodeSnippet = computed(() => `import OpenAI from 'openai'; const openai = new OpenAI({ - apiKey: 'sk-no-key-required', - baseURL: '${apiUrl}/v1', + baseURL: '${apiUrl}/v1',${ + parameters.value.api_key ? '\n apiKey: \''+parameters.value.api_key+'\'' : '' + } }) async function main() { const completion = await openai.chat.completions.create({ model: "${currentModel.value}", - messages=[${ + messages=[ + { role: 'system', content: '${session.value.prompt}'},${ (session.value.transcript as Array).map(([role, [entry]]) => { if (role.toLowerCase() === '{{user}}') { return `\n { role: 'user', content: '${entry.content}' }` @@ -83,12 +87,17 @@ main(); const shSnippet = computed(() => `invoke_url='${apiUrl}/v1/chat/completions' -authorization_header='Authorization: Bearer sk-no-key-required' +${ +parameters.value.api_key + ? 'authorization_header=\'Authorization: Bearer '+parameters.value.api_key+'\'' + : '' +} accept_header='Accept: application/json' content_type_header='Content-Type: application/json' data=$'{ - "messages": [${ + "messages": [ + { "role": "system", "content": "${session.value.prompt}" },${ (session.value.transcript as Array).map(([role, [entry]]) => { if (role.toLowerCase() === '{{user}}') { return `\n { "role": "user", "content": "${entry.content}" }` @@ -109,8 +118,11 @@ data=$'{ }' response=$(curl --silent -i -w "\\n%{http_code}" --request POST \\ - --url "$invoke_url" \\ - --header "$authorization_header" \\ + --url "$invoke_url" \\${ + parameters.value.api_key + ? '\n --header "$authorization_header" \\' + : '' +} --header "$accept_header" \\ --header "$content_type_header" \\ --data "$data"