darkfallse

Untitled

Oct 11th, 2023 (edited)
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. // -------------------------------------------------------- Generate LLM Stream Route
  2.  
  3. import Replicate from "replicate";
  4. import { ReplicateStream, streamToResponse } from 'ai';
  5. import { NextApiRequest, NextApiResponse } from 'next';
  6.  
  7. const replicate = new Replicate({ auth: process.env.REPLICATE_API_KEY });
  8.  
  9. export default async function handler(
  10. req: NextApiRequest,
  11. res: NextApiResponse
  12. ) {
  13.  
  14. const { prompt } = await req.body as { prompt: string };
  15. const prediction = await replicate.predictions.create({
  16. version: "a52e56fee2269a78c9279800ec88898cecb6c8f1df22a6483132bea266648f00",
  17. input: {
  18. prompt: `You are a helpful ai that can complete chats, text and give good responses in a logical manner . Give me completion for this : ${prompt}`,
  19. // max_new_tokens: 256,
  20. },
  21. stream: true,
  22. });
  23.  
  24. // Convert the response into a friendly text-stream
  25. const stream = await ReplicateStream(prediction);
  26. // Respond with the stream
  27.  
  28. console.log("stream", stream) // stream ReadableStream { locked: false, state: 'readable', supportsBYOB: false }
  29. return streamToResponse(stream, res);
  30.  
  31. }
  32.  
  33. // -------------------------------------------------------- UI Compoennt
  34.  
  35. 'use client'
  36. import React from 'react'
  37. import { type Content, EditorContent, useEditor } from '@tiptap/react'
  38. import StarterKit from '@tiptap/starter-kit'
  39. import Text from '@tiptap/extension-text'
  40. import TipTapMenuBar from './TipTapMenuBar'
  41. import { useDebounce } from '../../lib/useDebounce'
  42. import { useCompletion } from 'ai/react'
  43.  
  44. export const runtime = 'experimental-edge';
  45.  
  46. const TipTapEditor = () => {
  47. const [editorState, setEditorState] = React.useState('')
  48. const { complete , completion , stop } = useCompletion({
  49. api : 'api/replicateAPI/generateLLMStream',
  50. onResponse: res => {
  51. // trigger something when the response starts streaming in
  52. // e.g. if the user is rate limited, you can show a toast
  53. if (res.status === 429) {
  54. console.log('You are being rate limited. Please try again later.', res);
  55. }
  56. },
  57. onFinish: () => {
  58. // do something with the completion result
  59. console.log('Successfully generated completion!');
  60. },
  61. })
  62. const customText = Text.extend({
  63. addKeyboardShortcuts() {
  64. return {
  65. 'Shift-a': () => {
  66. const prompt = this.editor.getText().split(' ').slice(-30).join(' ')
  67. console.log(prompt)
  68. complete(prompt)
  69. return true;
  70. },
  71. }
  72. }
  73. })
  74. const editor = useEditor({
  75. autofocus: true,
  76. extensions: [StarterKit, customText],
  77. content: editorState,
  78. onUpdate: ({ editor }) => {
  79. setEditorState(editor.getHTML())
  80. },
  81. })
  82.  
  83. const debouncedEditorState = useDebounce(editorState, 500)
  84.  
  85. const lastCompletion = React.useRef("")
  86. React.useEffect(() => {
  87. console.log('completion', completion)
  88. if (!completion || !editor) return;
  89. const diff = completion.slice(lastCompletion.current.length);
  90. lastCompletion.current = completion;
  91. editor.commands.insertContent(diff);
  92. }, [completion, editor]);
  93.  
  94. React.useEffect(() => {
  95. console.log('debouncedEditorState', debouncedEditorState)
  96. }, [debouncedEditorState])
  97.  
  98. return (
  99. <>
  100. <div className=''>
  101. { editor && <TipTapMenuBar editor={editor}/>}
  102. </div>
  103. <div className='p-4 prose dark:prose-invert'>
  104. <EditorContent editor={editor} />
  105. </div>
  106. <div onClick={stop}>Button</div>
  107. <div>
  108. {completion}
  109. </div>
  110. </>
  111. )
  112. }
  113.  
  114. export default TipTapEditor
Advertisement
Add Comment
Please, Sign In to add comment