-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(llms): first iteration, oracle search flow (#1311)
- Loading branch information
1 parent
7f1ed2b
commit 233c0f6
Showing
22 changed files
with
550 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
@import '../../../components/containerGradient/saber/index.module'; | ||
|
||
.wrapper { | ||
position: relative; | ||
|
||
* { | ||
color: white; | ||
} | ||
|
||
> div:last-of-type { | ||
@include saber('purple', left, 'color'); | ||
@include saber('purple', right, 'color'); | ||
} | ||
} | ||
|
||
.left, | ||
.right { | ||
position: absolute; | ||
z-index: 1; | ||
padding: 10px; | ||
top: calc(45% - (40px / 2)); | ||
} | ||
|
||
.left { | ||
right: 100%; | ||
display: flex; | ||
align-items: center; | ||
gap: 7px 10px; | ||
font-size: 14px; | ||
} | ||
|
||
.right { | ||
padding-top: 15px; | ||
left: 100%; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { createOpenRouter } from '@openrouter/ai-sdk-provider'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { generateText } from 'ai'; | ||
import { Link, useNavigate, useSearchParams } from 'react-router-dom'; | ||
import { Display } from 'src/components'; | ||
import useAddToIPFS from 'src/features/ipfs/hooks/useAddToIPFS'; | ||
import { routes } from 'src/routes'; | ||
import TextMarkdown from 'src/components/TextMarkdown'; | ||
import { useHover } from 'src/hooks/useHover'; | ||
import Loader2 from 'src/components/ui/Loader2'; | ||
import useGetIPFSHash from 'src/features/ipfs/hooks/useGetIPFSHash'; | ||
import { isCID } from 'src/utils/ipfs/helpers'; | ||
import { isDevEnv } from 'src/utils/dev'; | ||
import { testVar } from '.'; | ||
import styles from './LLMSpark.module.scss'; | ||
|
||
// WIP | ||
|
||
const provider = createOpenRouter({ | ||
['a' + 'piK' + 'ey']: `sk-or-v1-${atob(testVar)}`, | ||
}); | ||
|
||
const modelName = isDevEnv() | ||
? 'meta-llama/llama-3-8b-instruct:free' | ||
: 'openai/gpt-4o-mini'; | ||
|
||
const model = provider.chat(modelName); | ||
|
||
export async function llmRequest(prompt) { | ||
const { text } = await generateText({ | ||
model, | ||
prompt, | ||
}); | ||
|
||
return text; | ||
} | ||
|
||
function useLLMResponse(text) { | ||
const { data, isLoading, error } = useQuery( | ||
['llm', text], | ||
async () => { | ||
return llmRequest(`what is ${text}`); | ||
}, | ||
{ | ||
enabled: Boolean(text), | ||
} | ||
); | ||
|
||
return { | ||
data, | ||
isLoading, | ||
error, | ||
}; | ||
} | ||
|
||
export function useIsLLMPageParam() { | ||
const [searchParams] = useSearchParams(); | ||
|
||
const isLLM = searchParams.get('llm') === 'true'; | ||
|
||
return isLLM; | ||
} | ||
|
||
export function LLMAvatar() { | ||
return ( | ||
<div | ||
style={{ | ||
display: 'flex', | ||
alignItems: 'center', | ||
}} | ||
> | ||
<img | ||
src="https://robohash.org/llama" | ||
style={{ | ||
width: '20px', | ||
height: '20px', | ||
borderRadius: '50%', | ||
marginRight: '5px', | ||
}} | ||
/> | ||
{model.modelId} | ||
</div> | ||
); | ||
} | ||
|
||
function LLMSpark({ searchText }: { searchText: string }) { | ||
const { data, isLoading } = useLLMResponse(searchText); | ||
const { execute } = useAddToIPFS(data); | ||
|
||
const [ref, hovering] = useHover(); | ||
|
||
const cid = useGetIPFSHash(data); | ||
|
||
const navigate = useNavigate(); | ||
|
||
if (isCID(searchText)) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Link | ||
ref={ref} | ||
className={styles.wrapper} | ||
to={routes.oracle.ask.getLink(cid)} | ||
onClick={async (e) => { | ||
e.preventDefault(); | ||
|
||
const hash = await execute(); | ||
|
||
navigate(`${routes.oracle.ask.getLink(hash)}?llm=true`); | ||
}} | ||
> | ||
{hovering && ( | ||
<div className={styles.left}> | ||
<LLMAvatar /> | ||
</div> | ||
)} | ||
<Display color={isLoading ? 'yellow' : 'purple'}> | ||
{isLoading && <Loader2 text="llm is generating response" />} | ||
{data && <TextMarkdown preview>{data}</TextMarkdown>} | ||
</Display> | ||
</Link> | ||
); | ||
} | ||
|
||
export default LLMSpark; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
const var1 = 'NzFlZjJiOGVlZDRlNDYyOWNjZDY5YzQzZDljMjEwMzJhZGJhYmI5MGI1ZW'; | ||
const var2 = 'ZiMzBlZmVjMmRhZDE5MjNiODFmNQ'; | ||
|
||
export const testVar = var1 + var2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
.wrapper { | ||
.particle { | ||
min-height: 200px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/containers/ipfs/components/SoulCompanion/SoulCompanion.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
.wrapper { | ||
max-width: 62%; | ||
margin: 20px auto; | ||
} | ||
|
||
.itemLinks { | ||
display: flex; | ||
list-style-type: none; | ||
justify-content: center; | ||
font-size: 16px; | ||
} | ||
|
||
.itemText { | ||
font-size: 14px; | ||
display: block; | ||
} | ||
|
||
.itemLink { | ||
margin: 0 5px; | ||
|
||
// white-space: nowrap; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
max-width: 140px; | ||
display: -webkit-box; | ||
-webkit-line-clamp: 1; | ||
-webkit-box-orient: vertical; | ||
} |
Oops, something went wrong.