-
-
Notifications
You must be signed in to change notification settings - Fork 602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Opensource examples - Runpod w/Text-Generation-WebUI API Endpoint #247
Changes from 25 commits
934d1f8
0f9e91c
ab672c3
39c615e
2fbca81
695cec8
1f2b85d
b144220
88f2a12
d0e5b6d
5efe277
41eb729
f3e2050
8fd2746
1d938ff
81c3e06
edec614
ffd392f
21b24fc
f3ba8bd
a2f05fc
ed4d42f
f4ef55d
b399561
b5626f6
c1ab8a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Instructor with open source models | ||
Instructor works with Open source model providers that support the [OpenAI API chat endpoint](https://platform.openai.com/docs/api-reference/chat) | ||
|
||
See examples README [here](https://github.com/jxnl/instructor/tree/main/examples/open_source_examples) | ||
|
||
# Currently tested open source model providers | ||
- [OpenRouter](https://openrouter.ai/) | ||
- [Perplexity](https://www.perplexity.ai/) | ||
- [RunPod TheBloke LLMs](https://github.com/TheBlokeAI/dockerLLM/blob/main/README_Runpod_LocalLLMsUI.md) ** | ||
|
||
|
||
** This utilizes text-generation-webui w/ Openai plugin under the hood. |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,68 @@ | ||||||||||||
import os | ||||||||||||
import instructor | ||||||||||||
from openai import OpenAI | ||||||||||||
from pydantic import BaseModel, Field | ||||||||||||
from typing import Optional | ||||||||||||
from instructor import Mode | ||||||||||||
|
||||||||||||
# Extract API key from environment | ||||||||||||
runpod_api_key = os.environ.get("RUNPOD_API_KEY") | ||||||||||||
assert runpod_api_key, "RUNPOD_API_KEY is not set in environment variables" | ||||||||||||
|
||||||||||||
# Base URL for OpenAI | ||||||||||||
runpod_base_url = os.environ.get("RUNPOD_BASE_URL") | ||||||||||||
assert runpod_base_url, "RUNPOD_BASE_URL is not set in environment variables" | ||||||||||||
Comment on lines
+13
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar issue as above with - assert runpod_base_url, "RUNPOD_BASE_URL is not set in environment variables"
+ if not runpod_base_url:
+ raise ValueError("RUNPOD_BASE_URL is not set in environment variables") Committable suggestion
Suggested change
|
||||||||||||
|
||||||||||||
# Initialize OpenAI client | ||||||||||||
client = instructor.patch( | ||||||||||||
OpenAI(api_key=runpod_api_key, base_url=runpod_base_url), | ||||||||||||
mode=Mode.JSON, | ||||||||||||
) | ||||||||||||
|
||||||||||||
# For direct reference here. See https://docs.perplexity.ai/docs/model-cards for updates | ||||||||||||
# Recommended is pplx-70b-chat | ||||||||||||
models = [ | ||||||||||||
"TheBloke_OpenHermes-2.5-Mistral-7B-GPTQ", | ||||||||||||
] | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||||||||||||
|
||||||||||||
data = [ | ||||||||||||
"Brandon is 33 years old. He works as a solution architect.", | ||||||||||||
"Jason is 25 years old. He is the GOAT.", | ||||||||||||
"Dominic is 45 years old. He is retired.", | ||||||||||||
"Jenny is 72. She is a wife and a CEO.", | ||||||||||||
"Holly is 22. She is an explorer.", | ||||||||||||
"There onces was a prince, named Benny. He ruled for 10 years, which just ended. He started at 22.", | ||||||||||||
"Simon says, why are you 22 years old marvin?", | ||||||||||||
] | ||||||||||||
|
||||||||||||
|
||||||||||||
if __name__ == "__main__": | ||||||||||||
|
||||||||||||
class UserDetail(BaseModel): | ||||||||||||
name: str = Field(description="Name extracted from the text") | ||||||||||||
age: int = Field(description="Age extracted from the text") | ||||||||||||
occupation: Optional[str] = Field( | ||||||||||||
default=None, description="Occupation extracted from the text" | ||||||||||||
) | ||||||||||||
Comment on lines
+36
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||||||||||||
|
||||||||||||
for content in data: | ||||||||||||
try: | ||||||||||||
user = client.chat.completions.create( | ||||||||||||
response_model=UserDetail, | ||||||||||||
model="TheBloke_OpenHermes-2.5-Mistral-7B-GPTQ", | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The model name is hardcoded in the - model="TheBloke_OpenHermes-2.5-Mistral-7B-GPTQ",
+ model=models[0], # Assuming models list is intended to be used here Committable suggestion
Suggested change
|
||||||||||||
messages=[ | ||||||||||||
{ | ||||||||||||
"role": "system", | ||||||||||||
"content": "You are an expert at outputting json. You output valid JSON.", | ||||||||||||
}, | ||||||||||||
{ | ||||||||||||
"role": "user", | ||||||||||||
"content": f"Extract the user details from the following text: {content}. Match your response to the following schema: {UserDetail.model_json_schema()}", | ||||||||||||
}, | ||||||||||||
], | ||||||||||||
max_retries=1, | ||||||||||||
) | ||||||||||||
print(f"Result: {user}") | ||||||||||||
except Exception as e: | ||||||||||||
print(f"Error: {e}") | ||||||||||||
Comment on lines
+60
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error handling in the loop is basic and only prints the error message. Consider implementing a more robust error handling strategy, such as logging the errors to a file or an error tracking system. |
||||||||||||
continue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of
assert
for checking environment variables can cause the application to crash if the variables are not set. Consider handling this with proper error messages and exit the application gracefully.Committable suggestion