Skip to content

Commit

Permalink
strict prompting and longer response: backend
Browse files Browse the repository at this point in the history
  • Loading branch information
pinocchio-life-like committed Jun 1, 2024
1 parent 692f511 commit a3f9ab4
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 5 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"packageManager": "[email protected]",
"dependencies": {
"@babel/runtime": "^7.21.0",
"@instructor-ai/instructor": "^1.3.0",
"@manypkg/cli": "^0.21.0",
"@prisma/extension-accelerate": "^0.6.2",
"check-dependency-version-consistency": "^4.1.0",
Expand All @@ -101,7 +102,8 @@
"postinstall-postinstall": "^2.1.0",
"prettier": "^3.2.5",
"react-native-action-button": "^2.8.5",
"typescript": "^5.3.3"
"typescript": "^5.3.3",
"zod": "^3.23.8"
},
"lint-staged": {
"client/**/*.{js,jsx,ts,tsx}": [
Expand Down
14 changes: 12 additions & 2 deletions packages/app/components/chat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,24 @@ const ChatModalTrigger: React.FC<ChatModalTriggerProps> = ({ itemTypeId }) => {
>
<RImage
source={{
// TODO: Update this to use the PackRat logo from the assets folder
// TODO: Update this to use the intended chat logo
uri: 'https://raw.githubusercontent.com/andrew-bierman/PackRat/4ad449702c088e505c4b484219121d365150f971/packages/app/assets/chat-svgrepo-com%20(1).svg',
width: 50,
height: 50,
}}
width={40}
height={40}
style={styles.logo}
style={{
...styles.logo,
shadowColor: '#000',
shadowOffset: {
width: 0,
height: 2,
},
shadowOpacity: 0.75,
shadowRadius: 1.24,
elevation: 3,
}}
alt="PackRat Logo"
/>
</TouchableOpacity>
Expand Down
19 changes: 19 additions & 0 deletions server/src/services/openAi/getAIResponseService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import OpenAI from 'openai';
import Instructor from '@instructor-ai/instructor';
import { z } from 'zod';
import { Conversation } from '../../drizzle/methods/Conversation';
import { User } from '../../drizzle/methods/User';

Expand All @@ -24,9 +26,11 @@ export const getAIResponseService = async (

const conversationClass = new Conversation();
const userClass = new User();

const openai = new OpenAI({
apiKey: openAIAPIKey,
});

const user = await userClass.findUser({ userId });

if (!user) {
Expand All @@ -53,11 +57,26 @@ export const getAIResponseService = async (
return accumulator;
}, []);

// a detailed system message to instruct the model about its role
const systemMessage = {
role: 'system',
content: `You are a helpful assistant that provides hiking advice.
You should only provide advice related to hiking and not generate any other type of content whatsoever.
If a user asks for help outside of the hiking domain, respond with: "Sorry, I can not help with that 😅!"
Additionally, make your responses more detailed when the user asks relevant hiking-related questions.`,
};

// Add the system message to the beginning of the messages array
messages.unshift(systemMessage);

// Add the user message
messages.push({ role: 'user', content: userInput });

const response = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
messages,
temperature: 0.5, // Lower temperature to make output more deterministic
// min_tokens: 100, // Limit the length of the response this doesnt exist still
});

const aiResponse = response.choices?.[0]?.message?.content?.trim() || '';
Expand Down
59 changes: 57 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4313,6 +4313,19 @@ __metadata:
languageName: node
linkType: hard

"@instructor-ai/instructor@npm:^1.3.0":
version: 1.3.0
resolution: "@instructor-ai/instructor@npm:1.3.0"
dependencies:
zod-stream: "npm:1.0.3"
zod-validation-error: "npm:^2.1.0"
peerDependencies:
openai: ">=4.28.0"
zod: ">=3.22.4"
checksum: 10/29174fc7edfb6febbdddd68798160380ea4fe9e16c2e39853057c3b6f827ecda1e0334bffca8ec96cf477b51ee6e4088977ffffeb4c5034da91dd5cdc5f230f9
languageName: node
linkType: hard

"@internationalized/date@npm:^3.5.3":
version: 3.5.3
resolution: "@internationalized/date@npm:3.5.3"
Expand Down Expand Up @@ -28571,6 +28584,7 @@ __metadata:
resolution: "packrat-world@workspace:."
dependencies:
"@babel/runtime": "npm:^7.21.0"
"@instructor-ai/instructor": "npm:^1.3.0"
"@manypkg/cli": "npm:^0.21.0"
"@prisma/extension-accelerate": "npm:^0.6.2"
check-dependency-version-consistency: "npm:^4.1.0"
Expand All @@ -28585,6 +28599,7 @@ __metadata:
prettier: "npm:^3.2.5"
react-native-action-button: "npm:^2.8.5"
typescript: "npm:^5.3.3"
zod: "npm:^3.23.8"
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -30187,6 +30202,13 @@ __metadata:
languageName: node
linkType: hard

"ramda@npm:^0.29.0":
version: 0.29.1
resolution: "ramda@npm:0.29.1"
checksum: 10/5824249efa18f3c013ea0bae6cdcc167ef357fff5441586b7ad98759e902b1397297d92b50bdc317f49fe73fa6b0e17f806f4d0f7a95270f05c25afa733a8fc5
languageName: node
linkType: hard

"randexp@npm:^0.5.3":
version: 0.5.3
resolution: "randexp@npm:0.5.3"
Expand Down Expand Up @@ -31957,6 +31979,17 @@ __metadata:
languageName: node
linkType: hard

"schema-stream@npm:^3.1.0":
version: 3.1.0
resolution: "schema-stream@npm:3.1.0"
dependencies:
ramda: "npm:^0.29.0"
peerDependencies:
zod: 3.22.4
checksum: 10/ecc601d625504a1c89e88a4d7e615c1c457258dad69e1a07246d5ebe691ca6b4648079621d307584d4a6d2dc409818abf7b11de027f286e859994c752ba43c41
languageName: node
linkType: hard

"schema-utils@npm:^2.6.5":
version: 2.7.1
resolution: "schema-utils@npm:2.7.1"
Expand Down Expand Up @@ -36966,6 +36999,19 @@ __metadata:
languageName: node
linkType: hard

"zod-stream@npm:1.0.3":
version: 1.0.3
resolution: "zod-stream@npm:1.0.3"
dependencies:
schema-stream: "npm:^3.1.0"
zod-to-json-schema: "npm:^3.22.3"
peerDependencies:
openai: ">=4.24.1"
zod: ">=3.22.4"
checksum: 10/5959c263d74c04dc173246885da579119edbe3a488c4c11f2114a771cb876a6a31d83e9008f741304847846a36eadd928795db0714fbdfdb6306ca7ecc486400
languageName: node
linkType: hard

"zod-to-json-schema@npm:3.20.3":
version: 3.20.3
resolution: "zod-to-json-schema@npm:3.20.3"
Expand All @@ -36975,7 +37021,7 @@ __metadata:
languageName: node
linkType: hard

"zod-to-json-schema@npm:^3.20.0":
"zod-to-json-schema@npm:^3.20.0, zod-to-json-schema@npm:^3.22.3":
version: 3.23.0
resolution: "zod-to-json-schema@npm:3.23.0"
peerDependencies:
Expand All @@ -36994,7 +37040,16 @@ __metadata:
languageName: node
linkType: hard

"zod@npm:^3.20.2, zod@npm:^3.20.6, zod@npm:^3.22.3, zod@npm:^3.22.4":
"zod-validation-error@npm:^2.1.0":
version: 2.1.0
resolution: "zod-validation-error@npm:2.1.0"
peerDependencies:
zod: ^3.18.0
checksum: 10/fdd3a9fdf176571404abf22af3af330e89227c3cb2e7092d61d0c6dfc8dd688648025346303585a03db51aa466efef5ebb8bdf14dbe630ccdedd5d0aa00741ab
languageName: node
linkType: hard

"zod@npm:^3.20.2, zod@npm:^3.20.6, zod@npm:^3.22.3, zod@npm:^3.22.4, zod@npm:^3.23.8":
version: 3.23.8
resolution: "zod@npm:3.23.8"
checksum: 10/846fd73e1af0def79c19d510ea9e4a795544a67d5b34b7e1c4d0425bf6bfd1c719446d94cdfa1721c1987d891321d61f779e8236fde517dc0e524aa851a6eff1
Expand Down

0 comments on commit a3f9ab4

Please sign in to comment.