-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rune): extend soul companion with openai
- Loading branch information
Showing
14 changed files
with
157 additions
and
79 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
5 changes: 4 additions & 1 deletion
5
src/containers/ipfs/components/SoulCompanion/soulCompanion.module.scss
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,35 +1,76 @@ | ||
/* eslint-disable import/prefer-default-export */ | ||
/* eslint-disable import/no-unused-modules */ | ||
import axios from 'axios'; | ||
import axios, { ResponseType } from 'axios'; | ||
|
||
// https://platform.openai.com/docs/models/overview | ||
// gpt-3.5-turbo | ||
|
||
// https://platform.openai.com/docs/api-reference/chat/create | ||
export const promptToOpenAI = async ( | ||
prompt: string, | ||
type OpenAiMessage = { | ||
role: 'system' | 'user' | 'assistant'; | ||
content: string; | ||
}; | ||
|
||
interface OpenAIParams { | ||
model: string; | ||
messages: OpenAiMessage[]; | ||
[key: string]: any; | ||
} | ||
|
||
const defaultOpenAIParams: Partial<OpenAIParams> = { | ||
model: 'gpt-3.5-turbo', | ||
}; | ||
|
||
export const openAICompletion = async ( | ||
messages: OpenAiMessage[], | ||
apiKey: string, | ||
params: any = { | ||
model: 'text-davinci-003', // 'gpt-3.5-turbo', | ||
maxTokens: 500, | ||
stop: '.', | ||
n: 1, | ||
} | ||
) => { | ||
//prompt: `Complete this sentence: "${input}"`, | ||
const response = await axios.post( | ||
'https://api.openai.com/v1/completions', | ||
{ | ||
prompt, | ||
params: Partial<OpenAIParams> = {} | ||
): Promise<string | AsyncIterable<string>> => { | ||
const requestOptions = { | ||
method: 'post', | ||
url: 'https://api.openai.com/v1/chat/completions', | ||
data: { | ||
messages, | ||
...defaultOpenAIParams, | ||
...params, | ||
}, | ||
{ | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${apiKey}`, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${apiKey}`, | ||
}, | ||
responseType: (params.stream ? 'stream' : 'json') as ResponseType, | ||
}; | ||
|
||
const response = await axios(requestOptions); | ||
|
||
if (!params.stream) { | ||
// Non-streaming request | ||
console.log('response', response); | ||
return response.data.choices[0].message.content; | ||
} else { | ||
// Streaming request | ||
const asyncIterable: AsyncIterable<string> = { | ||
[Symbol.asyncIterator]: async function* () { | ||
let result = ''; | ||
for await (const chunk of response.data) { | ||
const str = chunk.toString(); | ||
const lines = str.split('\n').filter((line) => line.trim() !== ''); | ||
for (const line of lines) { | ||
const message = line.replace(/^data: /, ''); | ||
if (message === '[DONE]') { | ||
return; | ||
} | ||
try { | ||
const parsed = JSON.parse(message); | ||
result += parsed.choices[0].delta.content; | ||
yield parsed.choices[0].delta.content; | ||
} catch (error) { | ||
console.error('Error parsing stream message:', message, error); | ||
} | ||
} | ||
} | ||
}, | ||
} | ||
); | ||
console.log('response', response); | ||
return response.data.choices[0].text; | ||
}; | ||
|
||
return asyncIterable; | ||
} | ||
}; |
This file contains 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
This file contains 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
Oops, something went wrong.