-
Notifications
You must be signed in to change notification settings - Fork 307
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Python deps, base64 impl, add examples (#426)
* Fix python api deps * Fix deps and add examples * Update readmes with credit * Fix links * Fix base64 examples and configure higher body limit * Fix base64 examples and configure higher body limit * Cleaner way to load from file, base64, http * Update example * Add loading from local img docs * Update docs * Bump version to 0.1.18 * Clippy format and docs * Default profile for py is release * Update readme to build in verbose
- Loading branch information
1 parent
922d94e
commit 0728b33
Showing
19 changed files
with
306 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from mistralrs import Runner, Which, ChatCompletionRequest, VisionArchitecture | ||
import base64 | ||
|
||
runner = Runner( | ||
which=Which.VisionPlain( | ||
model_id="microsoft/Phi-3-vision-128k-instruct", | ||
tokenizer_json=None, | ||
repeat_last_n=64, | ||
arch=VisionArchitecture.Phi3V, | ||
), | ||
) | ||
|
||
FILENAME = "picture.jpg" | ||
with open(FILENAME, "rb") as image_file: | ||
encoded_string = base64.b64encode(image_file.read()).decode("utf-8") | ||
|
||
res = runner.send_chat_completion_request( | ||
ChatCompletionRequest( | ||
model="phi3v", | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{ | ||
"type": "image_url", | ||
"image_url": { | ||
"url": str(encoded_string), | ||
}, | ||
}, | ||
{ | ||
"type": "text", | ||
"text": "<|image_1|>\nWhat is shown in this image?", | ||
}, | ||
], | ||
} | ||
], | ||
max_tokens=256, | ||
presence_penalty=1.0, | ||
top_p=0.1, | ||
temperature=0.1, | ||
) | ||
) | ||
print(res.choices[0].message.content) | ||
print(res.usage) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from mistralrs import Runner, Which, ChatCompletionRequest, VisionArchitecture | ||
import base64 | ||
|
||
runner = Runner( | ||
which=Which.VisionPlain( | ||
model_id="microsoft/Phi-3-vision-128k-instruct", | ||
tokenizer_json=None, | ||
repeat_last_n=64, | ||
arch=VisionArchitecture.Phi3V, | ||
), | ||
) | ||
|
||
FILENAME = "picture.jpg" | ||
|
||
res = runner.send_chat_completion_request( | ||
ChatCompletionRequest( | ||
model="phi3v", | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{ | ||
"type": "image_url", | ||
"image_url": { | ||
"url": FILENAME, | ||
}, | ||
}, | ||
{ | ||
"type": "text", | ||
"text": "<|image_1|>\nWhat is shown in this image?", | ||
}, | ||
], | ||
} | ||
], | ||
max_tokens=256, | ||
presence_penalty=1.0, | ||
top_p=0.1, | ||
temperature=0.1, | ||
) | ||
) | ||
print(res.choices[0].message.content) | ||
print(res.usage) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import requests | ||
import httpx | ||
import textwrap, json | ||
import base64 | ||
|
||
|
||
def log_response(response: httpx.Response): | ||
request = response.request | ||
print(f"Request: {request.method} {request.url}") | ||
print(" Headers:") | ||
for key, value in request.headers.items(): | ||
if key.lower() == "authorization": | ||
value = "[...]" | ||
if key.lower() == "cookie": | ||
value = value.split("=")[0] + "=..." | ||
print(f" {key}: {value}") | ||
print(" Body:") | ||
try: | ||
request_body = json.loads(request.content) | ||
print(textwrap.indent(json.dumps(request_body, indent=2), " ")) | ||
except json.JSONDecodeError: | ||
print(textwrap.indent(request.content.decode(), " ")) | ||
print(f"Response: status_code={response.status_code}") | ||
print(" Headers:") | ||
for key, value in response.headers.items(): | ||
if key.lower() == "set-cookie": | ||
value = value.split("=")[0] + "=..." | ||
print(f" {key}: {value}") | ||
|
||
|
||
BASE_URL = "http://localhost:1234/v1" | ||
|
||
# Enable this to log requests and responses | ||
# openai.http_client = httpx.Client( | ||
# event_hooks={"request": [print], "response": [log_response]} | ||
# ) | ||
|
||
FILENAME = "picture.jpg" | ||
with open(FILENAME, "rb") as image_file: | ||
encoded_string = base64.b64encode(image_file.read()).decode("utf-8") | ||
|
||
headers = { | ||
"Content-Type": "application/json", | ||
} | ||
|
||
payload = { | ||
"model": "phi3v", | ||
"messages": [ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{ | ||
"type": "image_url", | ||
"image_url": { | ||
"url": str(encoded_string), | ||
}, | ||
}, | ||
{ | ||
"type": "text", | ||
"text": "<|image_1|>\nWhat is shown in this image?", | ||
}, | ||
], | ||
} | ||
], | ||
"max_tokens": 300, | ||
} | ||
|
||
response = requests.post(f"{BASE_URL}/chat/completions", headers=headers, json=payload) | ||
print(response.json()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import requests | ||
import httpx | ||
import textwrap, json | ||
import base64 | ||
|
||
|
||
def log_response(response: httpx.Response): | ||
request = response.request | ||
print(f"Request: {request.method} {request.url}") | ||
print(" Headers:") | ||
for key, value in request.headers.items(): | ||
if key.lower() == "authorization": | ||
value = "[...]" | ||
if key.lower() == "cookie": | ||
value = value.split("=")[0] + "=..." | ||
print(f" {key}: {value}") | ||
print(" Body:") | ||
try: | ||
request_body = json.loads(request.content) | ||
print(textwrap.indent(json.dumps(request_body, indent=2), " ")) | ||
except json.JSONDecodeError: | ||
print(textwrap.indent(request.content.decode(), " ")) | ||
print(f"Response: status_code={response.status_code}") | ||
print(" Headers:") | ||
for key, value in response.headers.items(): | ||
if key.lower() == "set-cookie": | ||
value = value.split("=")[0] + "=..." | ||
print(f" {key}: {value}") | ||
|
||
|
||
BASE_URL = "http://localhost:1234/v1" | ||
|
||
# Enable this to log requests and responses | ||
# openai.http_client = httpx.Client( | ||
# event_hooks={"request": [print], "response": [log_response]} | ||
# ) | ||
|
||
FILENAME = "picture.jpg" | ||
|
||
headers = { | ||
"Content-Type": "application/json", | ||
} | ||
|
||
payload = { | ||
"model": "phi3v", | ||
"messages": [ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{ | ||
"type": "image_url", | ||
"image_url": { | ||
"url": FILENAME, | ||
}, | ||
}, | ||
{ | ||
"type": "text", | ||
"text": "<|image_1|>\nWhat is shown in this image?", | ||
}, | ||
], | ||
} | ||
], | ||
"max_tokens": 300, | ||
} | ||
|
||
response = requests.post(f"{BASE_URL}/chat/completions", headers=headers, json=payload) | ||
print(response.json()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.