Skip to content

Commit

Permalink
Merge pull request #26 from telekom/include-json-fetching
Browse files Browse the repository at this point in the history
Include json fetching
  • Loading branch information
FPullem authored Dec 17, 2024
2 parents e18bbff + 0e1a2a2 commit 0363162
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/04_Model Serving/API Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ pip install openai
print(model.id)
```
</TabItem>

</TabItem>

<TabItem value="json" label="JSON">

<FetchJson url="https://llm-server.llmhub.t-systems.net/v1/chat/completions" method="POST" body={{ key: 'value' }} />

</TabItem>

</Tabs>

:::info
Expand Down
46 changes: 46 additions & 0 deletions src/components/FetchJson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React, { useEffect, useState } from 'react';

const FetchJson = ({ url, method = 'GET', body = null }) => {
const [data, setData] = useState(null);
const [error, setError] = useState(null);

useEffect(() => {
console.log(`Fetching data from ${url} with method ${method}`);
fetch(url, {
method: method,
headers: {
'Content-Type': 'application/json'
},
body: method === 'POST' ? JSON.stringify(body) : null,
})
.then(response => {
if (!response.ok) {
return response.text().then(text => {
throw new Error(`Network response was not ok: ${response.status} ${response.statusText} - ${text}`);
});
}
const contentType = response.headers.get('content-type');
if (contentType && contentType.includes('application/json')) {
return response.json();
} else {
throw new Error('Response is not JSON');
}
})
.then(data => {
console.log('Data fetched successfully:', data);
setData(data);
})
.catch(error => {
console.error('Error fetching JSON:', error);
setError(error.message);
});
}, [url, method, body]);

return (
<pre>
{error ? `Error: ${error}` : data ? JSON.stringify(data, null, 2) : 'Loading...'}
</pre>
);
};

export default FetchJson;

0 comments on commit 0363162

Please sign in to comment.