@semoss\sdk-react
@semoss\sdk-react is a small react package that accelerates the process of building an deploying an app.
Getting Started:
First, install the sdk using a package manager (or your favorite cdn):
npm install @semoss/sdk-react
Second, install dependencies using a package manager:
npm install @semoss/sdk
Next, import the InsightProvider. This provider will wrap your components and provide an Insight to all of it's children. Insights are temporal workspaces that allow end users to script and interact with a model, storage engine, or database.
import { InsightProvider } from '@semoss/sdk-react';
const App = (props) => {
const { children } = props;
return <InsightProvider>{children}</InsightProvider>;
};
Once the application is wrapped, You can access the insight through the useInsight hook;
import { useInsight } from '@semoss/sdk-react';
const Child = (props) => {
const { children } = props;
const {
isInitialized,
isAuthorized,
error,
system,
actions,
} = useInsight();
return <div>{children}</div>;
};
Now you are ready to go. You can do things like
const login = (username, password) => {
const success = await actions.login({
type: 'native',
username: username,
password: password,
});
console.log(success);
};
const loginWithOauth = (provider: 'ms' ) => {
const resp = await insight.actions.login({
type: 'oauth',
provider: provider
})
console.log(resp)
}
const logout = (username, password) => {
const success = await actions.logout();
console.log(success);
};
- Query a LLM and return a result
const ask = (question) => {
const { output } = await actions.askModel(MODEL_ID, question);
console.log(output);
};
import { partial, runPixel } from '@semoss/sdk';
const { actions, insightId } = useInsight()
const askWithStream = async () => {
let isCollecting = false;
const collectMessage = async () => {
if (!isCollecting) {
return;
}
try {
const output = await partial(insightId);
if (output.message && output.message.total) {
setAnswer(output.message.total);
}
setTimeout(() => collectMessage(), 1000);
} catch (e) {
}
}
isCollecting = true;
setTimeout(() => collectMessage(), 500);
const { errors, pixelReturn } = await runPixel(
`LLM(engine=["001510f8-b86e-492e-a7f0-41299775e7d9"], command=["<encode>${question}</encode>"]);`,
insightId,
);
isCollecting = false
}
const getMovies = () => {
const { output } = await actions.queryDatabase(
DATABASE_ID,
'select * from movie',
);
console.log(output);
};
const sum = (num1, num2) => {
const { output } = await actions.runPy(`${num1} + ${num2}`);
console.log(output);
};
const upload = (file, path) => {
const { output } = await actions.upload(file, path);
console.log(output);
};
const download = (path) => {
const { output } = await actions.download(path);
console.log(output);
};
Tips and Tricks
Here are a few tips and tricks that can help streamline the development process.
Development Environment
Note: We recommend manually setting the environment only in development mode.
You can setup a development environment and use access keys to authenticate with the app server. Generate the keys on the server and then update the Env module. See:
import { Env } from '@semoss/sdk';
Env.update({
MODULE: '',
ACCESS_KEY: '',
SECRET_KEY: '',
APP: '',
});
Note: Please do not commit your keys. Instead externalize your keys to a .env and load them in as environment variables during development
Python
The app server allows you to write custom python to power your app. You can initialize your python environment by:
The sdk will load python via an external file.
def sayHello(name):
print(f'Hello {name}')
Set the option on initialize:
initialize({
python: {
type: 'file',
path: './hello.py',
alias: 'smss',
},
});
Set the option on initialize:
const py = `
def sayHello(name):
print(f'Hello {name}')
`;
initialize({
python: {
type: 'script',
script: py,
alias: 'smss',
},
});
Next you can the preloaded python methods by calling the runPy action. See
const hello = (name) => {
const { output } = await actions.runPy(
`smss.sayHello(${name})`,
);
console.log(output);
};