Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
nakasyou committed Nov 30, 2024
1 parent a2d96d9 commit cda0a47
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 134 deletions.
2 changes: 1 addition & 1 deletion src/integrations/cloudflare/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function createExports(manifest: import('astro').SSRManifest) {

const honoApp = new Hono()

honoApp.use(basicAuth({verifyUser: (username, password, c) => true ,}))
honoApp.use(basicAuth({ verifyUser: (username, password, c) => true }))

honoApp.all('/*', async (c) => {
const res = await app.render(c.req.raw)
Expand Down
12 changes: 6 additions & 6 deletions src/islands/ai-quiz/QuizScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import {
For,
Show,
createEffect,
createMemo,
createSignal,
For,
onMount,
Show,
} from 'solid-js'
import { QuizManager, type GeneratedQuiz } from './quiz-manager'
import type { MargedNoteData } from '../note/components/notes-utils'
import { QuizDB, type Quizzes } from './storage'
import type { QuizContent } from './constants'
import { shuffle } from '../../utils/arr'
import { icon } from '../../utils/icons'
import type { MargedNoteData } from '../note/components/notes-utils'
import type { TextNoteData } from '../note/components/notes/TextNote/types'
import type { QuizContent } from './constants'
import { type GeneratedQuiz, QuizManager } from './quiz-manager'
import { QuizDB, type Quizzes } from './storage'
import './QuizScreen.css'

export const finish = () => {
Expand Down
10 changes: 5 additions & 5 deletions src/islands/ai-quiz/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createSignal, onMount, Show } from 'solid-js'
import type { NoteLoadType } from '../note/note-load-types'
import { loadNoteFromType } from '../shared/storage'
import { load } from '../note/utils/file-format'
import { Show, createSignal, onMount } from 'solid-js'
import type { MargedNoteData } from '../note/components/notes-utils'
import { Spinner } from '../note/components/utils/Spinner'
import { finish, QuizScreen } from './QuizScreen'
import type { NoteLoadType } from '../note/note-load-types'
import { load } from '../note/utils/file-format'
import { loadNoteFromType } from '../shared/storage'
import { QuizScreen, finish } from './QuizScreen'

export const Navbar = () => {}

Expand Down
12 changes: 6 additions & 6 deletions src/islands/ai-quiz/quiz-manager.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { parse, safeParse } from 'valibot'
import { shuffle } from '../../utils/arr'
import type { MargedNoteData } from '../note/components/notes-utils'
import type { TextNoteData } from '../note/components/notes/TextNote/types'
import { getGoogleGenerativeAI } from '../shared/gemini'
import { sha256 } from '../shared/hash'
import { generate } from '../shared/llm'
import {
CONTENT_SCHEMA,
PROMPT_TO_GENERATE_SELECT_QUIZ,
type QuizContent,
} from './constants'
import type { MargedNoteData } from '../note/components/notes-utils'
import type { TextNoteData } from '../note/components/notes/TextNote/types'
import type { QuizDB, Quizzes } from './storage'
import { shuffle } from '../../utils/arr'
import { sha256 } from '../shared/hash'
import { generate } from '../shared/llm'
const generateQuizzesFromAI = async (text: string): Promise<QuizContent[]> => {
const gemini = getGoogleGenerativeAI()
if (!gemini) {
Expand All @@ -20,7 +20,7 @@ const generateQuizzesFromAI = async (text: string): Promise<QuizContent[]> => {
const response = await generate({
systemPrompt: PROMPT_TO_GENERATE_SELECT_QUIZ,
userPrompt: text,
jsonMode: true
jsonMode: true,
})

let json: unknown
Expand Down
1 change: 0 additions & 1 deletion src/islands/ai-quiz/utils/generate-quizzes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export const quizzesGenerator = () => {
return null
}


const model = gemini.getGenerativeModel({
model: 'gemini-1.5-flash',
generationConfig: {
Expand Down
32 changes: 19 additions & 13 deletions src/islands/note/components/notes/TextNote/AI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
onMount,
untrack,
} from 'solid-js'
import { Spinner } from '../../utils/Spinner'
import { generate as generateLLM, generateStream } from '../../../../shared/llm'
import { Spinner } from '../../utils/Spinner'

const markdownParser = markdownIt()

Expand Down Expand Up @@ -145,10 +145,12 @@ export const FromText = (props: {
const generate = async () => {
setIsPreprocessing(true)
props.setStream({
generate: () =>generateStream({
systemPrompt: 'ユーザーの指示に基づき、暗記の手助けになる赤シート用文章を生成しなさい。赤シートで隠すべき単語は、Markdownの太字機能で表現しなさい。隠す必要がない場所には太字は使わないでください。太字の場所はユーザーに見えません。また、単語の一覧を箇条書きにすることはしないでください。',
userPrompt: getPrompt()
})
generate: () =>
generateStream({
systemPrompt:
'ユーザーの指示に基づき、暗記の手助けになる赤シート用文章を生成しなさい。赤シートで隠すべき単語は、Markdownの太字機能で表現しなさい。隠す必要がない場所には太字は使わないでください。太字の場所はユーザーに見えません。また、単語の一覧を箇条書きにすることはしないでください。',
userPrompt: getPrompt(),
}),
})
setIsPreprocessing(false)
}
Expand Down Expand Up @@ -213,12 +215,13 @@ export const FromImage = (props: {

const generate = async () =>
await generateStream({
systemPrompt: '画像を抽出し、そっくりそのまま書き出しなさい。省略せずに画像の文字全てを書き出すこと。画像に書いていないことは書かないこと。また、ユーザーからの指示があれば、条件を満たす場所をMarkdownの太字で表現しなさい。',
image: {mimeType: file.type,data: b64,},
userPrompt: ''
systemPrompt:
'画像を抽出し、そっくりそのまま書き出しなさい。省略せずに画像の文字全てを書き出すこと。画像に書いていないことは書かないこと。また、ユーザーからの指示があれば、条件を満たす場所をMarkdownの太字で表現しなさい。',
image: { mimeType: file.type, data: b64 },
userPrompt: '',
})
props.setStream({
generate
props.setStream({
generate,
})
setIsPreprocessing(false)
}
Expand All @@ -235,9 +238,12 @@ export const FromImage = (props: {
>
<div class="h-[70dvh] max-h-[70dvh] grid grid-rows-2 grid-cols-1 md:grid-rows-1 md:grid-cols-2">
<div>
<div style={{
'background-image': `url("${getScanedImageURL()}")`
}} class="w-full h-full bg-no-repeat bg-center bg-contain" />
<div
style={{
'background-image': `url("${getScanedImageURL()}")`,
}}
class="w-full h-full bg-no-repeat bg-center bg-contain"
/>
</div>
<div class="h-full">
<label class="h-full flex flex-col">
Expand Down
44 changes: 25 additions & 19 deletions src/islands/shared/llm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { StepMap } from "@tiptap/pm/transform"
import { SSETransformStream } from "./sse"
import { StepMap } from '@tiptap/pm/transform'
import { SSETransformStream } from './sse'

export interface GenerateInit {
userPrompt: string
Expand All @@ -11,26 +11,32 @@ export interface GenerateInit {
jsonMode?: boolean
}
export const generate = async (init: GenerateInit) => {
const res = await fetch('https://mobile-app-contest-nanoha.deno.dev/generate', {
method: 'POST',
body: JSON.stringify({ ...init, stream: false }),
headers: {
'Content-Type': 'application/json'
}
})
const res = await fetch(
'https://mobile-app-contest-nanoha.deno.dev/generate',
{
method: 'POST',
body: JSON.stringify({ ...init, stream: false }),
headers: {
'Content-Type': 'application/json',
},
},
)
if (!res.ok) {
throw new Error(`error: ${await res.text()}`)
}
return await res.text()
}
export const generateStream = async (init: GenerateInit) => {
const res = await fetch('https://mobile-app-contest-nanoha.deno.dev/generate', {
method: 'POST',
body: JSON.stringify({ ...init, stream: true }),
headers: {
'Content-Type': 'application/json'
}
})
const res = await fetch(
'https://mobile-app-contest-nanoha.deno.dev/generate',
{
method: 'POST',
body: JSON.stringify({ ...init, stream: true }),
headers: {
'Content-Type': 'application/json',
},
},
)
if (!res.ok) {
throw new Error('Error')
}
Expand All @@ -39,7 +45,7 @@ export const generateStream = async (init: GenerateInit) => {
}
const stream = res.body.pipeThrough(new SSETransformStream())
const reader = await stream.getReader()
return async function * () {
return (async function* () {
while (true) {
const { value, done } = await reader.read()
if (value) {
Expand All @@ -49,5 +55,5 @@ export const generateStream = async (init: GenerateInit) => {
return
}
}
}()
}
})()
}
Loading

0 comments on commit cda0a47

Please sign in to comment.