Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Plate Editor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Projekt HeadlessCMS-WS24-25
Plate Editor
Commits
99ed16ca
Commit
99ed16ca
authored
2 months ago
by
Benedikt Schäfer
Browse files
Options
Downloads
Patches
Plain Diff
Remove unused code
parent
724f1f78
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/components/editor/use-chat.ts
+1
-114
1 addition, 114 deletions
src/components/editor/use-chat.ts
with
1 addition
and
114 deletions
src/components/editor/use-chat.ts
+
1
−
114
View file @
99ed16ca
...
@@ -54,39 +54,6 @@ export const useChat = () => {
...
@@ -54,39 +54,6 @@ export const useChat = () => {
});
});
};
};
// export const useChat = () => {
// const { keys, model } = useSettings();
// return useBaseChat({
// id: 'editor',
// api: '/api/ai/command',
// body: {
// // !!! DEMO ONLY: don't use API keys client-side
// // apiKey: keys.openai,
// model: model.value,
// },
// fetch: async (input, init) => {
// const res = await fetch(input, init);
// if (!res.ok) {
// // Mock the API response. Remove it when you implement the route /api/ai/command
// await new Promise((resolve) => setTimeout(resolve, 400));
// const stream = fakeStreamText();
// return new Response(stream, {
// headers: {
// Connection: 'keep-alive',
// 'Content-Type': 'text/plain',
// },
// });
// }
// return res;
// },
// });
// };
// Used for testing. Remove it after implementing useChat api.
// Used for testing. Remove it after implementing useChat api.
const
fakeStreamText
=
({
const
fakeStreamText
=
({
chunkCount
=
10
,
chunkCount
=
10
,
...
@@ -124,84 +91,4 @@ const fakeStreamText = ({
...
@@ -124,84 +91,4 @@ const fakeStreamText = ({
controller
.
close
();
controller
.
close
();
},
},
});
});
};
};
\ No newline at end of file
const
streamOllamaText
=
async
({
messages
,
model
=
'
phi3
'
,
streamProtocol
=
'
data
'
,
}:
{
messages
:
any
[];
model
?:
string
;
streamProtocol
?:
'
data
'
|
'
text
'
;
})
=>
{
const
ollama
=
createOllama
({
baseURL
:
process
.
env
.
OLLAMA_BASE_URL
||
'
http://localhost:11434/api
'
});
const
encoder
=
new
TextEncoder
();
return
new
ReadableStream
({
async
start
(
controller
)
{
try
{
const
response
=
await
fetch
(
`
${
ollama
.
baseURL
}
/chat`
,
{
method
:
'
POST
'
,
headers
:
{
'
Content-Type
'
:
'
application/json
'
,
},
body
:
JSON
.
stringify
({
model
:
model
,
messages
:
messages
,
stream
:
true
,
}),
});
const
reader
=
response
.
body
?.
getReader
();
if
(
!
reader
)
throw
new
Error
(
'
No reader available
'
);
while
(
true
)
{
const
{
done
,
value
}
=
await
reader
.
read
();
if
(
done
)
break
;
const
chunk
=
new
TextDecoder
().
decode
(
value
);
const
lines
=
chunk
.
split
(
'
\n
'
).
filter
(
Boolean
);
for
(
const
line
of
lines
)
{
const
json
=
JSON
.
parse
(
line
);
if
(
streamProtocol
===
'
text
'
)
{
controller
.
enqueue
(
encoder
.
encode
(
json
.
message
?.
content
||
''
));
}
else
{
controller
.
enqueue
(
encoder
.
encode
(
`0:
${
JSON
.
stringify
(
json
.
message
?.
content
||
''
)}
\n`
)
);
}
}
}
if
(
streamProtocol
===
'
data
'
)
{
controller
.
enqueue
(
encoder
.
encode
(
`d:{"finishReason":"stop","usage":{"promptTokens":0,"completionTokens":0}}\n`
)
);
}
controller
.
close
();
}
catch
(
error
)
{
controller
.
error
(
error
);
}
},
});
};
// Usage in API route
export
async
function
POST
(
req
:
Request
)
{
const
{
messages
}
=
await
req
.
json
();
const
stream
=
await
streamOllamaText
({
messages
,
model
:
'
phi3
'
,
// or any other Ollama model
streamProtocol
:
'
data
'
});
return
new
StreamingTextResponse
(
stream
);
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment