Skip to content
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

Merged
merged 26 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
934d1f8
Added openrouter usage example inside open_source_examples folder
PhiBrandon Nov 26, 2023
0f9e91c
Updated comments on example
PhiBrandon Nov 26, 2023
ab672c3
Refactored code to utilize Maybe to wrap model.
PhiBrandon Nov 27, 2023
39c615e
Merge branch 'main' into opensource_examples
PhiBrandon Nov 27, 2023
2fbca81
removed unneccesary functions. added global client
PhiBrandon Nov 27, 2023
695cec8
Merge branch 'opensource_examples' of https://github.com/PhiBrandon/i…
PhiBrandon Nov 27, 2023
1f2b85d
Stored openrouter url in env. removed assertion.
PhiBrandon Nov 27, 2023
b144220
create_user_message update. removed retry loop.
PhiBrandon Nov 27, 2023
88f2a12
Merge branch 'jxnl:main' into opensource_examples
PhiBrandon Nov 28, 2023
d0e5b6d
update
jxnl Nov 28, 2023
5efe277
Added perplexity example.
PhiBrandon Nov 29, 2023
41eb729
Merge branch 'jxnl:main' into opensource_examples
PhiBrandon Nov 29, 2023
f3e2050
ran ruff format
PhiBrandon Nov 29, 2023
8fd2746
Merge branch 'opensource_examples' of https://github.com/PhiBrandon/i…
PhiBrandon Nov 29, 2023
1d938ff
edited assertion typo.
PhiBrandon Nov 29, 2023
81c3e06
Added README for setting up examples.
PhiBrandon Nov 29, 2023
edec614
typo fixed.
PhiBrandon Nov 29, 2023
ffd392f
typo fixed
PhiBrandon Nov 29, 2023
21b24fc
Added Runpod example and updated Readme
PhiBrandon Dec 1, 2023
f3ba8bd
Merge branch 'opensource_examples' of https://github.com/PhiBrandon/i…
PhiBrandon Dec 1, 2023
a2f05fc
Updated Readme
PhiBrandon Dec 1, 2023
ed4d42f
revised call to send proper messages.
PhiBrandon Dec 1, 2023
f4ef55d
Merge branch 'jxnl:main' into opensource_examples
PhiBrandon Dec 4, 2023
b399561
Added open source doc and updated mkdocs.yml
PhiBrandon Dec 4, 2023
b5626f6
markdown presentation cleanup
PhiBrandon Dec 4, 2023
c1ab8a3
Cleaned up comments and swapped out runpod model.
PhiBrandon Dec 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/examples/open_source.md
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.
15 changes: 14 additions & 1 deletion examples/open_source_examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,17 @@
1. Sign up for an Openrouter Account - https://www.perplexity.ai/
2. Create an API key - https://www.perplexity.ai/pplx-api
3. Add API key to environment - `export PERPLEXITY_API_KEY=your key here`
4. Add Openrouter API endpoint to environment - `export PERPLEXITY_BASE_URL=https://api.perplexity.ai` [See https://docs.perplexity.ai/reference/post_chat_completions for potential updates]
4. Add Openrouter API endpoint to environment - `export PERPLEXITY_BASE_URL=https://api.perplexity.ai` [See https://docs.perplexity.ai/reference/post_chat_completions for potential updates]

## Runpod
1. Sign up for a Runpod account - https://www.runpod.io/console/signup
2. Add credits, unfortunately no free tier. - https://www.runpod.io/console/user/billing
3. Navigate to templates page[Left selection menu], under `Official` click deploy on `RunPod TheBloke LLMs` template. - https://www.runpod.io/console/templates
4. Navigate to Community Cloud page [Left Selection menu], Click `Deploy` on a GPU with >=16 GB, 1x RTX 4000 Ada SFF works. - https://www.runpod.io/console/gpu-cloud
5. Click `Customize Deployment`, click the `Environment Variables` drop down, Enter the following Key/Values, then click `Set Overrides`, then click `Continue`, and finally `Deploy`.
- key=MODEL value=TheBloke/OpenHermes-2.5-Mistral-7B-GPTQ
- key=UI_ARGS value=--n-gpu-layers 100 --threads 1
6. Navigate to Pods[Left selection menu], wait until you see `Connect` button on the Pod you just deployed, click it. Right click `HTTP Service[Port 5000]` and copy the link address. - https://www.runpod.io/console/pods
- Add Runpod API endpoint to environment - `export RUNPOD_BASE_URL=your-runpod-link/v1` <-- Make sure to add v1 as well
- Add Runpod API key to environment - `export RUNPOD_API_KEY="None"` <-- This should be none.
7. When done running, stop instance by clicking the stop icon on the Pod page. - https://www.runpod.io/console/pods
68 changes: 68 additions & 0 deletions examples/open_source_examples/runpod.py
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"
Comment on lines +9 to +10
Copy link
Contributor

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.

- assert runpod_api_key, "RUNPOD_API_KEY is not set in environment variables"
+ if not runpod_api_key:
+     raise ValueError("RUNPOD_API_KEY is not set in environment variables")

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
runpod_api_key = os.environ.get("RUNPOD_API_KEY")
assert runpod_api_key, "RUNPOD_API_KEY is not set in environment variables"
runpod_api_key = os.environ.get("RUNPOD_API_KEY")
if not runpod_api_key:
raise ValueError("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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar issue as above with RUNPOD_BASE_URL. Replace the assert with a more graceful error handling mechanism.

- 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

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
runpod_base_url = os.environ.get("RUNPOD_BASE_URL")
assert runpod_base_url, "RUNPOD_BASE_URL is not set in environment variables"
runpod_base_url = os.environ.get("RUNPOD_BASE_URL")
if not runpod_base_url:
raise ValueError("RUNPOD_BASE_URL is not set in environment variables")


# 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",
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The models list is defined but not used in the code. If it's intended for future use, consider adding a comment to clarify its purpose, or remove it if it's not needed.


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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The UserDetail class is defined within the if __name__ == "__main__": block, which limits its scope. If this class is intended for use outside of this script, consider moving it to the module level.


for content in data:
try:
user = client.chat.completions.create(
response_model=UserDetail,
model="TheBloke_OpenHermes-2.5-Mistral-7B-GPTQ",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The model name is hardcoded in the client.chat.completions.create call. Consider using the models list or an environment variable to make it configurable.

- model="TheBloke_OpenHermes-2.5-Mistral-7B-GPTQ",
+ model=models[0],  # Assuming models list is intended to be used here

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
model="TheBloke_OpenHermes-2.5-Mistral-7B-GPTQ",
model=models[0], # Assuming models list is intended to be used here

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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ nav:
- Action Item and Dependency Mapping: 'examples/action_items.md'
- Multi-File Code Generation: 'examples/gpt-engineer.md'
- PII Data Sanitization: 'examples/pii.md'
- Open Source: 'examples/open_source.md'
- CLI Reference:
- "Introduction": "cli/index.md"
- "Finetuning GPT-3.5": "cli/finetune.md"
Expand Down
Loading