This repository was archived by the owner on Sep 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
feature: send message to assistants and assistants respond to message #32
Merged
GuiBibeau
merged 5 commits into
AkeruAI:main
from
multipletwigs:feature/send-message-to-assistant
Mar 11, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0cf9957
chore: added in dayjs for date sort comparision
multipletwigs 552061f
enh: added more complete types for gpt-4 adapter, and allow for using…
multipletwigs 279e43b
enh: allowed instructions for assistant, model selection when creatin…
multipletwigs 883ad3d
enh: denormalized role information of sender
multipletwigs f7775fd
enh: added in a thread run that saves responses from assistants
multipletwigs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { app } from "@/index"; | ||
import { test, expect, describe } from "bun:test"; | ||
import { getLastMessage } from "../../services/messageService"; | ||
import { Message } from "@/core/domain/messages"; | ||
import { createHumanUserForTesting } from "@/__tests__/utils"; | ||
|
||
describe.only("threadController", async () => { | ||
const token = await createHumanUserForTesting(); | ||
|
||
test("Run a created thread with a created assistant and save response from assistant", async () => { | ||
// Creating a new thread | ||
const thread_request = new Request("http://localhost:8080/thread", { | ||
headers: { | ||
authorization: `Bearer ${token}`, | ||
"Content-Type": "application/json", | ||
}, | ||
method: "POST", | ||
}); | ||
|
||
const thread_response = await app.handle(thread_request); | ||
const thread_response_json: any = await thread_response.json(); | ||
expect(thread_response_json).toHaveProperty('id') | ||
const thread_id = thread_response_json.id | ||
|
||
// Creating a new assistant | ||
const assistant_name = "Skater Assistant" | ||
const assistant_request = new Request("http://localhost:8080/assistant", { | ||
headers: { | ||
authorization: `Bearer ${token}`, | ||
"Content-Type": "application/json", | ||
}, | ||
method: "POST", | ||
body: JSON.stringify({ | ||
name: assistant_name, | ||
model: 'gpt-4', | ||
instruction: "You are a pro skater, give very short skating tips. Always respond with 'skate on'." | ||
}), | ||
}); | ||
const assistant_req = await app.handle(assistant_request) | ||
const assistant_req_json = await assistant_req.json() | ||
expect(assistant_req_json).toHaveProperty("id") | ||
const assistant_id = assistant_req_json.id | ||
|
||
// Running a thread | ||
const thread_run_request = new Request(`http://localhost:8080/thread/${thread_id}/run`, { | ||
headers: { | ||
authorization: `Bearer ${token}`, | ||
"Content-Type": "application/json", | ||
}, | ||
method: "POST", | ||
body: JSON.stringify({ | ||
assistant_id: assistant_id | ||
}), | ||
}); | ||
const run_response = await app.handle(thread_run_request) | ||
const run_json = await run_response.json() | ||
|
||
// expect run_response to have thread_id, and thread_id's latest message to be from assistant, with the content of 'skate on' | ||
expect(run_json).toHaveProperty('thread_id') | ||
expect(run_json).toHaveProperty('assistant_id') | ||
|
||
// get the latest message from thread id | ||
const lastMessage: Message = await getLastMessage(run_json.thread_id) | ||
expect(lastMessage.role).toBe('assistant') | ||
expect(lastMessage.content.toLocaleLowerCase()).toContain('skate on') | ||
}) | ||
|
||
|
||
}); |
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// run the thread with the associated assistant | ||
|
||
import { ThreadRun, ThreadRunRequest } from "@/core/domain/run"; | ||
import { getAssistantData } from "./assistantService"; | ||
import { getThread } from "./threadService"; | ||
import { createMessage, getAllMessage } from "./messageService"; | ||
import { gpt4Adapter } from "@/infrastructure/adaptaters/openai/gpt4Adapter"; | ||
import { Role } from "@/core/domain/roles"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
|
||
export async function runAssistantWithThread(runData: ThreadRunRequest) { | ||
// get all messages from the thread, and run it over to the assistant to get a response | ||
const { assistant_id, thread_id } = runData; | ||
const [assistantData, threadData] = await Promise.all([ | ||
getAssistantData(assistant_id), | ||
getThread(thread_id), | ||
]); | ||
|
||
// If no thread data or assistant data, an error should be thrown as we need both to run a thread | ||
if (!threadData || !assistantData) throw new Error("No thread or assistant found."); | ||
|
||
const everyMessage = await getAllMessage(threadData.id); | ||
// only get role and content from every message for context. | ||
// TODO: We should truncate the context to fit context window for selected model. | ||
const everyRoleAndContent = everyMessage.map((message) => { | ||
// special case for super_admin, which really should just be user | ||
return { | ||
role: message.role === "super_admin" ? "user" : ("assistant" as Role), | ||
content: message.content, | ||
}; | ||
}); | ||
|
||
// Calls the appropriate adapter based on what model the assistant uses | ||
if (assistantData.model === "gpt-4") { | ||
const gpt4AdapterRes: any = await gpt4Adapter( | ||
everyRoleAndContent, | ||
assistantData.instruction | ||
); | ||
|
||
const assistantResponse: string = gpt4AdapterRes.choices[0].message.content; | ||
|
||
// add assistant response to the thread | ||
await createMessage(assistant_id, thread_id, assistantResponse); | ||
|
||
const threadRunResponse: ThreadRun = { | ||
id: uuidv4(), | ||
assistant_id: assistant_id, | ||
thread_id: thread_id, | ||
created_at: new Date(), | ||
}; | ||
|
||
return threadRunResponse; | ||
} | ||
} |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we plan a default if ever somebody does not specify a model? That's something we can eventually do from a UI standpoint also.
feel free to ignore, not blocking a merge
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think from the UI standpoint we can do a default!