Skip to content
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

Assistant ai #353

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# You must first activate a Billing Account here: https://platform.openai.com/account/billing/overview
# Then get your OpenAI API Key here: https://platform.openai.com/account/api-keys
OPENAI_API_KEY=XXXXXXXX
OPENAI_API_KEY

# Generate a random secret: https://generate-secret.vercel.app/32 or `openssl rand -base64 32`
AUTH_SECRET=XXXXXXXX
AUTH_SECRET

# Instructions to create kv database here: https://vercel.com/docs/storage/vercel-kv/quickstart and
KV_URL=XXXXXXXX
KV_REST_API_URL=XXXXXXXX
KV_REST_API_TOKEN=XXXXXXXX
KV_REST_API_READ_ONLY_TOKEN=XXXXXXXX
KV_URL
KV_REST_API_URL
KV_REST_API_TOKEN
KV_REST_API_READ_ONLY_TOKEN
15 changes: 15 additions & 0 deletions attachment-svgrepo-com.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
126 changes: 126 additions & 0 deletions components/assistant/assistant.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import * as dotenv from "dotenv";
import { OpenAI } from 'openai';
import { Assistant } from "openai/resources/beta/assistants";
import { Uploadable } from "openai/uploads";

dotenv.config();

const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
});

interface AssistantParams {
name: string;
instructions: string;
}

export const createAssistant = async ({ name, instructions }: AssistantParams): Promise<string> => {
const assistant = await openai.beta.assistants.create({
name: name,
instructions: instructions,
tools: [{ type: "code_interpreter" }],
model: "gpt-3.5-turbo",
});

console.log("Assistant created:", assistant);
return JSON.stringify(assistant);
};

export const runAssistant = async ({ assistantId, threadId, instructions }:{assistantId:string,threadId:string,instructions:string}):Promise<string> => {
const run = await openai.beta.threads.runs.create(threadId, {
assistant_id: assistantId,
instructions: instructions,
});
console.log(`runAssistant: ${run.id}`)
return JSON.stringify(run);
};

export const getAllAssitants = async () => {
const myAssistants = await openai.beta.assistants.list({
order: "desc",
});

return myAssistants;
};

export const getAssistant = async (assistantId:string) => {
const assistant = await openai.beta.assistants.retrieve(assistantId);
return assistant;
};

//delete assistant
export const deleteAssistant = async (assistantId:string) => {
const response = await openai.beta.assistants.del(assistantId);
return response;
};

//check on the run thread
export const runCheck = async ({ threadId, runId }: { threadId: string, runId: string }) => {
console.log(`Inside runCheck, threadId: ${threadId}, runId: ${runId}`);
const check = await openai.beta.threads.runs.retrieve(threadId, runId);

console.log(`Check metadata: ${JSON.stringify(check.metadata)}`);
console.log(`Check error: ${JSON.stringify(check.last_error)}`);
console.log(`Check traceback: ${JSON.stringify(check.temperature)}`);

return check;
};

/* const getAllThreads = async () =>{

const threadList = await openai.beta.threads.
} */

//create new thrad and also run it up make it work
// const createAndRunThread = async (threadid:string) => {
// const newThread = await openai.beta.threads.createAndRun();
// return newThread;
// };

//create thread
export const createThread = async () :Promise<string>=> {
const thread = await openai.beta.threads.create();
return JSON.stringify(thread);
};

//get thread
export const getThread = async (threadId:string) => {
const thread = await openai.beta.threads.retrieve(threadId);
return thread;
};

//delete thread
export const deleteThread = async (threadId:string) => {
const response = await openai.beta.threads.del(threadId);
return response;
};

//create message
export const createMessage = async ({ threadId, content }:{threadId:string,content:string}):Promise<string> => {
const messages = await openai.beta.threads.messages.create(threadId, {
role: "user",
content: content,
});
return JSON.stringify(messages);
};

//get messages
export const getMessages = async ({threadId}:{threadId:string}) => {
const messages = await openai.beta.threads.messages.list(threadId, {
order: "asc",
limit: 100,
});
return messages;
};

// Upload a file with an "assistants" purpose

export const UploadFile = async (fileSrc:Uploadable) => {
const file = await openai.files.create({
file: fileSrc,
purpose: "assistants",
});
return file;
};


8 changes: 5 additions & 3 deletions components/prompt-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as React from 'react'
import Textarea from 'react-textarea-autosize'

import { useActions, useUIState } from 'ai/rsc'

import { useState } from 'react'
import { UserMessage } from './stocks/message'
import { type AI } from '@/lib/chat/actions'
import { Button } from '@/components/ui/button'
Expand All @@ -30,7 +30,8 @@ export function PromptForm({
const inputRef = React.useRef<HTMLTextAreaElement>(null)
const { submitUserMessage } = useActions()
const [_, setMessages] = useUIState<typeof AI>()

const [file, setFile] = useState(null);

React.useEffect(() => {
if (inputRef.current) {
inputRef.current.focus()
Expand Down Expand Up @@ -67,7 +68,7 @@ export function PromptForm({
}}
>
<div className="relative flex max-h-60 w-full grow flex-col overflow-hidden bg-background px-8 sm:rounded-md sm:border sm:px-12">
<Tooltip>
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="outline"
Expand Down Expand Up @@ -108,6 +109,7 @@ export function PromptForm({
</TooltipTrigger>
<TooltipContent>Send message</TooltipContent>
</Tooltip>

</div>
</div>
</form>
Expand Down
Binary file added images.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading