Skip to content

Commit

Permalink
added ts and curl code snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
bekossy committed May 1, 2024
1 parent ec49fd7 commit 2e09727
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 51 deletions.
12 changes: 7 additions & 5 deletions agenta-web/src/code_snippets/endpoints/fetch_config/curl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export default function cURLCode(uri: string, config_url: string, params: string): string {
return `curl -X POST ${uri} \
-H "Content-Type: application/json" \
-d '${params}'
`
export default function cURLCode(baseId: string, env_name: string): string {
return `
curl -X GET "https://cloud.agenta.ai/api/configs?base_id=${baseId}&environment_name=${env_name}" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
--connect-timeout 60
`
}
25 changes: 7 additions & 18 deletions agenta-web/src/code_snippets/endpoints/fetch_config/python.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
export default function pythonCode(uri: string, config_url: string, params: string): string {
export default function pythonCode(baseId: string, env_name: string): string {
return `
import requests
import json
# os.environ["AGENTA_API_KEY"] = "your_api_key" # Only when using cloud
# os.environ["HOST"] = "https://cloud.agenta.ai"
url = "${uri}"
config_url = "${config_url}"
config_response = requests.get(config_url)
config_data = config_response.json()
params = ${params}
params.update(config_data)
response = requests.post(url, json=params)
data = response.json()
print(json.dumps(data, indent=4))
`
from agenta import Agenta
ag = Agenta()
ag.get_config(base_id="${baseId}", environment="${env_name}", cache_timeout=600) # timeout 300 per default
`
}
49 changes: 37 additions & 12 deletions agenta-web/src/code_snippets/endpoints/fetch_config/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,44 @@
import {js as beautify} from "js-beautify"

export default function tsCode(uri: string, config_url: string, params: string): string {
export default function tsCode(baseId: string, env_name: string): string {
const codeString = `
const generate = async () => {
try {
const config_response = await axios.get("${config_url}");
let params = ${params}
const response = await axios.post("${uri}", {...params, ...config_response.data});
console.log(response.data);
} catch (error) {
console.error(error);
}
};
import axios from 'axios';
generate();
const getConfig = async (baseId: string, environmentName = null) => {
try {
const baseUrl = 'https://cloud.agenta.ai/api';
const params = {
base_id: baseId,
environment_name: environmentName
};
const response = await axios.get(baseUrl + "/configs", {
params: params,
headers: {
'Authorization': "Bearer YOUR_API_KEY",
'Content-Type': 'application/json'
},
timeout: 60000
});
if (response.status >= 200 && response.status < 300) {
return response.data;
} else if (response.status === 422) {
throw new Error(JSON.stringify(response.data));
}
} catch (error) {
if (error.response) {
console.error("API Error: " + error.response.status, error.response.data);
} else if (error.request) {
console.error('API Error: No response received', error.request);
} else {
console.error('Error', error.message);
}
throw error;
}
};
getConfig('${baseId}', '${env_name}').then(console.log).catch(console.error);
`

const formattedCodeString = beautify(codeString)
Expand Down
27 changes: 11 additions & 16 deletions agenta-web/src/pages/apps/[app_id]/endpoints/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ export default function VariantEndpoint() {
const {inputParams, isChatVariant, isLoading, isError, error} = useVariant(appId, variant!)
const createParams = (
inputParams: Parameter[] | null,
environmentName: string,
value: string | number,
environmentName?: string,
) => {
let mainParams: GenericObject = {}
let secondaryParams: GenericObject = {}
Expand All @@ -139,7 +139,9 @@ export default function VariantEndpoint() {
mainParams["inputs"] = secondaryParams
}

return JSON.stringify({...mainParams, environment: environmentName}, null, 2)
mainParams["environment"] = environmentName

return JSON.stringify(mainParams, null, 2)
}

if (isVariantsError) {
Expand All @@ -160,24 +162,17 @@ export default function VariantEndpoint() {
)
}

const invokeLlmAppParams = createParams(
inputParams,
"add_a_value",
selectedEnvironment?.name || "none",
)

const fetchConfigParams = createParams(inputParams, "add_a_value")

const params = createParams(inputParams, selectedEnvironment?.name || "none", "add_a_value")
const invokeLlmAppCodeSnippet: Record<string, string> = {
Python: invokeLlmApppythonCode(uri!, invokeLlmAppParams),
cURL: invokeLlmAppcURLCode(uri!, invokeLlmAppParams),
TypeScript: invokeLlmApptsCode(uri!, invokeLlmAppParams),
Python: invokeLlmApppythonCode(uri!, params),
cURL: invokeLlmAppcURLCode(uri!, params),
TypeScript: invokeLlmApptsCode(uri!, params),
}

const fetchConfigCodeSnippet: Record<string, string> = {
Python: fetchConfigpythonCode(uri!, uri!, fetchConfigParams),
cURL: fetchConfigcURLCode(uri!, uri!, fetchConfigParams),
TypeScript: fetchConfigtsCode(uri!, uri!, fetchConfigParams),
Python: fetchConfigpythonCode(variant.baseId, selectedEnvironment?.name!),
cURL: fetchConfigcURLCode(variant.baseId, selectedEnvironment?.name!),
TypeScript: fetchConfigtsCode(variant.baseId, selectedEnvironment?.name!),
}

const items: CollapseProps["items"] = [
Expand Down

0 comments on commit 2e09727

Please sign in to comment.