Skip to content

Commit b925bbf

Browse files
committed
add simple api example
1 parent 771261f commit b925bbf

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

examples/api_example.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import requests
2+
3+
ENDPOINT = "http://localhost:5001/api" # Please visit this link and read OpenAI documentation. It has a LOT more than what is shown here.
4+
5+
# This is a very basic example of how to use the KoboldCpp API in python.
6+
# For full documentation, you can launch KoboldCpp and read it at http://localhost:5001/api or view the web docs at https://lite.koboldai.net/koboldcpp_api
7+
# Note: KoboldCpp also provides a fully compatible /v1/completions and /v1/chat/completions API. You can use it as a direct replacement for any OpenAI API usecases.
8+
# Refer to https://platform.openai.com/docs/api-reference/completions and https://platform.openai.com/docs/api-reference/chat
9+
10+
payload = {
11+
"prompt": "Niko the kobold stalked carefully down the alley, his small scaly figure obscured by a dusky cloak that fluttered lightly in the cold winter breeze.",
12+
"max_context_length": 4096, # The maximum number of tokens in history that the AI can see. Increase for longer inputs.
13+
"max_length": 128, # How many token to be generated at maximum. It might stop before this if EOS is allowed.
14+
"rep_pen": 1.1, # Makes outputs less repetitive by penalizing repetition
15+
"rep_pen_range": 512, # The range to which to apply repetition penalty
16+
"rep_pen_slope": 0.7, # This number determains the strength of the repetition penalty over time
17+
"temperature": 0.8, # How random should the AI outputs be? Lower values make output more predictable.
18+
"top_k": 100, # Keep the X most probable tokens
19+
"top_p": 0.9, # Top P sampling / Nucleus Sampling, https://arxiv.org/pdf/1904.09751.pdf
20+
#"sampler_seed": 1337, # Use specific seed for text generation? This helps with consistency across tests.
21+
}
22+
23+
try:
24+
response = requests.post(f"{ENDPOINT}/v1/generate", json=payload) # Send prompt to API
25+
if response.status_code == 200:
26+
results = response.json()['results'] # Set results as JSON response
27+
text = results[0]['text'] # inside results, look in first group for section labeled 'text'
28+
print(text)
29+
except Exception as e:
30+
print(f"An error occurred: {e}")

tools/mtmd/clip.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2872,7 +2872,11 @@ bool clip_image_load_from_file(const char * fname, clip_image_u8 * img) {
28722872

28732873
//note that the memory here must be subsequently freed!
28742874
uint8_t* make_new_letterbox_img(uint8_t* input_image, int nx, int ny, int nc, int target_width, int target_height) {
2875-
int new_image_size = target_width * target_height * nc;
2875+
int new_image_size = (target_width * target_height * nc) + 512; //add some padding
2876+
if (target_width < nx || target_height < ny) {
2877+
printf("\nERROR: Target size smaller than input image\n");
2878+
return nullptr;
2879+
}
28762880
uint8_t* letterboxed_image = (uint8_t*)malloc(new_image_size);
28772881
if(letterboxed_image==nullptr)
28782882
{
@@ -2960,6 +2964,11 @@ bool clip_image_load_from_bytes(const unsigned char * bytes, size_t bytes_length
29602964
free(letterboxed_image);
29612965
letterboxed_image = nullptr;
29622966
}
2967+
else
2968+
{
2969+
//letterboxing failed. just use original image
2970+
clip_build_img_from_pixels(data, nx, ny, img);
2971+
}
29632972
}
29642973
else
29652974
{

0 commit comments

Comments
 (0)