Skip to content

Commit 418a2aa

Browse files
drew-harrisdensumesh
authored andcommitted
feat: llm fill-in
1 parent d543919 commit 418a2aa

2 files changed

Lines changed: 81 additions & 9 deletions

File tree

clients/trieve-shopify-extension/extensions/enrich-content-block/src/BlockExtension.tsx

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import {
44
BlockStack,
55
AdminBlock,
66
useApi,
7-
TextField,
7+
Text,
88
Button,
99
InlineStack,
1010
Banner,
1111
TextArea,
12+
Icon,
1213
} from "@shopify/ui-extensions-react/admin";
1314
import { TrieveProvider } from "./TrieveProvider";
1415
import { useChunkExtraContent } from "./useChunkExtraContent";
@@ -37,8 +38,13 @@ function App() {
3738
const [isSaving, setIsSaving] = useState(false);
3839
const [showSuccess, setShowSuccess] = useState(false);
3940

40-
const { extraContent, updateContent } =
41-
useChunkExtraContent(simplifiedProductId);
41+
const {
42+
extraContent,
43+
updateContent,
44+
generateAIDescription,
45+
loading,
46+
aiLoading,
47+
} = useChunkExtraContent(simplifiedProductId);
4248

4349
useEffect(() => {
4450
if (extraContent) {
@@ -64,11 +70,28 @@ function App() {
6470
Content saved successfully
6571
</Banner>
6672
)}
67-
<TextArea
68-
label="Product Content"
69-
value={content}
70-
onChange={setContent}
71-
/>
73+
<BlockStack>
74+
<InlineStack inlineAlignment="space-between" blockAlignment="end">
75+
<Text>Extra Content</Text>
76+
<Button
77+
disabled={aiLoading}
78+
variant="tertiary"
79+
onPress={generateAIDescription}
80+
>
81+
<InlineStack blockAlignment="center" gap="small small">
82+
<Icon name="WandMinor" />
83+
{aiLoading ? "Generating..." : "Generate AI Description"}
84+
</InlineStack>
85+
</Button>
86+
</InlineStack>
87+
<TextArea
88+
rows={4}
89+
disabled={loading}
90+
label=""
91+
value={content}
92+
onChange={setContent}
93+
/>
94+
</BlockStack>
7295
<InlineStack gap="base" inlineAlignment="end">
7396
<Button onPress={handleSave} disabled={isSaving}>
7497
{isSaving ? "Saving..." : "Save Content"}

clients/trieve-shopify-extension/extensions/enrich-content-block/src/useChunkExtraContent.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { useTrieve } from "./TrieveProvider";
44
export const useChunkExtraContent = (productId: string | undefined) => {
55
const trieve = useTrieve();
66
const [extraContent, setExtraContent] = useState<string>("");
7+
const [loading, setLoading] = useState(true);
8+
const [aiLoading, setAILoading] = useState(false);
79

810
// Fetch current product extra content chunk
911
const getData = async () => {
@@ -28,21 +30,65 @@ export const useChunkExtraContent = (productId: string | undefined) => {
2830
} catch {
2931
setExtraContent("");
3032
}
33+
setLoading(false);
34+
};
35+
36+
const generateAIDescription = async () => {
37+
if (!productId) {
38+
console.info("Tried to generate AI description without id");
39+
return;
40+
}
41+
setLoading(true);
42+
setAILoading(true);
43+
const topic = await trieve.createTopic({
44+
owner_id: "shopify-enrich-content-block",
45+
first_user_message: "Describe this product",
46+
name: "Shopify Enrich Content Block",
47+
});
48+
49+
const message = await trieve.createMessage({
50+
topic_id: topic.id,
51+
new_message_content:
52+
"Describe this product. Generate a description for an online shop. Keep it to 3 sentences maximum. Do not include an introduction or welcome message",
53+
use_group_search: true,
54+
filters: {
55+
must: [
56+
{
57+
field: "group_tracking_ids",
58+
match_all: [productId],
59+
},
60+
],
61+
},
62+
llm_options: {
63+
stream_response: false,
64+
},
65+
});
66+
67+
const response = message.split("||").at(1);
68+
if (!response) {
69+
console.error("No response from AI");
70+
return;
71+
}
72+
setExtraContent(response);
73+
setLoading(false);
74+
setAILoading(false);
3175
};
3276

3377
const updateContent = async (content: string) => {
3478
if (!productId) {
3579
console.info("Tried to update product content without id");
3680
return;
3781
}
82+
setLoading(true);
3883

39-
const result = await trieve.createChunk({
84+
await trieve.createChunk({
4085
chunk_html: content,
4186
tracking_id: `${productId}-pdp-content`,
4287
upsert_by_tracking_id: true,
4388
weight: 100,
4489
group_tracking_ids: [productId],
4590
});
91+
setLoading(false);
4692
};
4793

4894
useEffect(() => {
@@ -55,5 +101,8 @@ export const useChunkExtraContent = (productId: string | undefined) => {
55101
return {
56102
extraContent,
57103
updateContent,
104+
generateAIDescription,
105+
loading,
106+
aiLoading,
58107
};
59108
};

0 commit comments

Comments
 (0)