Skip to content

Commit

Permalink
Merge pull request #2292 from Agenta-AI/AGE-1332/fix-prompt-fetching-…
Browse files Browse the repository at this point in the history
…code

fix(frontend): prompt fetching code
  • Loading branch information
mmabrouk authored Nov 22, 2024
2 parents 51ce815 + d0163a3 commit 6b98e63
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 54 deletions.
25 changes: 18 additions & 7 deletions agenta-web/src/code_snippets/endpoints/fetch_config/curl.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
export default function cURLCode(baseId: string, env_name: string): string {
return `
curl -X GET "${process.env.NEXT_PUBLIC_AGENTA_API_URL}/api/configs?base_id=${baseId}&environment_name=${env_name}" \\
-H "Authorization: Bearer YOUR_API_KEY" \\
-H "Content-Type: application/json" \\
--connect-timeout 60
`
export default function cURLCode(appName: string, env_name: string): string {
return `curl -L '${process.env.NEXT_PUBLIC_AGENTA_API_URL}/api/variants/configs/fetch' \\
-H "Authorization: Bearer YOUR_API_KEY" \\
-H 'Content-Type: application/json' \\
-H 'Accept: application/json' \\
-d '{
"environment_ref": {
"slug": "${env_name}",
"version": null,
"id": null
},
"application_ref": {
"slug": "${appName}",
"version": null,
"id": null
}
}'
`
}
22 changes: 13 additions & 9 deletions agenta-web/src/code_snippets/endpoints/fetch_config/python.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
export default function pythonCode(baseId: string, env_name: string): string {
export default function pythonCode(appName: string, env_name: string): string {
return `
# os.environ["AGENTA_API_KEY"] = "your_api_key" # Only when using cloud
# os.environ["AGENTA_HOST"] = "${process.env.NEXT_PUBLIC_AGENTA_API_URL}"
import os
import agenta as ag
from agenta import Agenta
ag = Agenta()
ag.get_config(base_id="${baseId}",
environment="${env_name}",
cache_timeout=600) # timeout 300 per default
`
os.environ["AGENTA_API_KEY"] = "your_api_key" # Only when using cloud
os.environ["AGENTA_HOST"] = "${process.env.NEXT_PUBLIC_AGENTA_API_URL}"
ag.init()
config = ag.ConfigManager.get_from_registry(
app_slug="${appName}",
environment_slug="${env_name}"
)
print(config)
`
}
64 changes: 29 additions & 35 deletions agenta-web/src/code_snippets/endpoints/fetch_config/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,38 @@
import {js as beautify} from "js-beautify"

export default function tsCode(baseId: string, env_name: string): string {
const codeString = `
import axios from 'axios';
export default function tsCode(appName: string, env_name: string): string {
const codeString = `import axios from 'axios';
const getConfig = async (baseId: string, environmentName: string) => {
try {
const baseUrl = '${process.env.NEXT_PUBLIC_AGENTA_API_URL}/api';
const params = {
base_id: baseId,
environment_name: environmentName
};
const getConfig = async (appName: string, environmentSlug: string) => {
const baseUrl = 'https://oss.agenta.ai/api/variants/configs/fetch';
const response = await axios.get(baseUrl + "/configs", {
params: params,
headers: {
'Authorization': "Bearer YOUR_API_KEY",
'Content-Type': 'application/json'
},
timeout: 60000
});
try {
const response = await axios.post(baseUrl, {
environment_ref: {
slug: environmentSlug,
version: null,
id: null,
},
application_ref: {
slug: appName,
version: null,
id: null,
},
}, {
headers: {
'Authorization': "Bearer YOUR_API_KEY",
'Content-Type': 'application/json',
'Accept': 'application/json',
},
});
if (response.status >= 200 && response.status < 300) {
return response.data;
} else if (response.status === 422) {
throw new Error(JSON.stringify(response.data));
}
} catch (error: any) {
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;
}
};
return response.data;
} catch {
throw new Error('Failed to fetch configuration.');
}
};
getConfig('${baseId}', '${env_name}').then(console.log).catch(console.error);
getConfig('demo', 'production').then(console.log).catch(console.error);
`

const formattedCodeString = beautify(codeString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {useVariant} from "@/lib/hooks/useVariant"
import {isDemo} from "@/lib/helpers/utils"
import {dynamicComponent} from "@/lib/helpers/dynamic"
import VariantPopover from "../variants/VariantPopover"
import {useAppsData} from "@/contexts/app.context"

const DeploymentHistoryModal: any = dynamicComponent(
"pages/overview/deployments/DeploymentHistoryModal",
Expand Down Expand Up @@ -124,6 +125,7 @@ const DeploymentDrawer = ({
const classes = useStyles()
const router = useRouter()
const appId = router.query.app_id as string
const {currentApp} = useAppsData()
const [selectedLang, setSelectedLang] = useState("python")
const [uri, setURI] = useState<string | null>(null)
const [variant, setVariant] = useState<Variant | null>(null)
Expand Down Expand Up @@ -165,9 +167,9 @@ const DeploymentDrawer = ({
}

const fetchConfigCodeSnippet: Record<string, string> = {
python: fetchConfigpythonCode(variant?.baseId!, selectedEnvironment?.name!),
bash: fetchConfigcURLCode(variant?.baseId!, selectedEnvironment?.name!),
typescript: fetchConfigtsCode(variant?.baseId!, selectedEnvironment?.name!),
python: fetchConfigpythonCode(currentApp?.app_name!, selectedEnvironment?.name!),
bash: fetchConfigcURLCode(currentApp?.app_name!, selectedEnvironment?.name!),
typescript: fetchConfigtsCode(currentApp?.app_name!, selectedEnvironment?.name!),
}

return (
Expand Down

0 comments on commit 6b98e63

Please sign in to comment.