-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from DataChefHQ/feature/aws-llm-controller
feat: adding aws controller for llm application
- Loading branch information
Showing
30 changed files
with
636 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
set -euo pipefail | ||
|
||
python -m mypy ./src | ||
python -m pyright . | ||
python -m mypy ./tests | ||
python -m pytest -vv |
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,7 @@ | ||
name: dspy_application | ||
runtime: | ||
name: python | ||
options: | ||
toolchain: pip | ||
virtualenv: venv | ||
description: A minimal Dspy application |
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,31 @@ | ||
import os | ||
from controller import AwsDspyController | ||
from damavand.environment import Environment | ||
|
||
controller = AwsDspyController( | ||
name="my-dspy", | ||
region="eu-west-1", | ||
) | ||
|
||
|
||
def lambda_handler(event, context): | ||
return controller.build_or_run( | ||
app_id=event.get("app_id", "default"), | ||
question=event.get("question", "What llm are you?"), | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
if controller.environment == Environment.LOCAL: | ||
# run the lambda handler locally | ||
event = { | ||
"app_id": os.environ.get("APP_ID", "default"), | ||
"question": os.environ.get("QUESTION", "What llm are you?"), | ||
} | ||
context = {} | ||
|
||
if response := lambda_handler(event, context): | ||
print(response) | ||
else: | ||
# aws automatically calls lambda_handler | ||
pass |
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,14 @@ | ||
import json | ||
import dspy | ||
|
||
|
||
def default_question(connection: dspy.OpenAI, question: str) -> dict: | ||
dspy.settings.configure(lm=connection) | ||
predict = dspy.Predict("question -> answer") | ||
answer = predict(question=question) | ||
|
||
return { | ||
"statusCode": 200, | ||
"headers": {"Content-Type": "application/json"}, | ||
"body": json.dumps({"response": answer}), | ||
} |
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,64 @@ | ||
import dspy | ||
from typing import Callable, Optional | ||
|
||
from damavand.base.controllers.base_controller import runtime | ||
from damavand.cloud.aws.controllers.llm import AwsLlmController | ||
|
||
import applications | ||
|
||
|
||
API_KEY = "EMPTY" | ||
|
||
|
||
class AwsDspyController(AwsLlmController): | ||
def __init__( | ||
self, | ||
name, | ||
region: str, | ||
model: Optional[str] = None, | ||
tags: dict[str, str] = {}, | ||
**kwargs, | ||
) -> None: | ||
super().__init__(name, region, model, tags, **kwargs) | ||
self.applications: dict[str, Callable] = { | ||
"default": applications.default_question, | ||
} | ||
|
||
@property | ||
@runtime | ||
def connection(self) -> dspy.OpenAI: | ||
"""Return the dspy OpenAI model.""" | ||
|
||
return dspy.OpenAI( | ||
model=self.model_id, | ||
api_base=f"{self.base_url}/", | ||
api_key=API_KEY, | ||
model_type="chat", | ||
) | ||
|
||
@runtime | ||
def run_application(self, app_id: str, question: str, **kwargs) -> dict: | ||
"""Run the specified application.""" | ||
|
||
return self.applications[app_id](question, **kwargs) | ||
|
||
@runtime | ||
def build_or_run(self, **kwargs) -> None | dict: | ||
""" | ||
Build or run the application based on the execution mode. | ||
Parameters | ||
---------- | ||
kwargs | ||
arguments to be passed to the application. Check the `run_application` method for more information. | ||
Returns | ||
------- | ||
None | dict | ||
If the execution mode is runtime, return the output of the application otherwise None. | ||
""" | ||
|
||
if self.is_runtime_execution: | ||
self.run_application(**kwargs) | ||
else: | ||
self.provision() |
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,5 @@ | ||
-e ../../../damavand | ||
pulumi | ||
boto3 | ||
dspy-ai | ||
sagemaker |
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,7 @@ | ||
name: llm-openai-client | ||
runtime: | ||
name: python | ||
options: | ||
toolchain: pip | ||
virtualenv: venv | ||
description: A simple llm application that uses OpenAI's client. |
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,28 @@ | ||
from damavand.cloud.aws.controllers.llm import AwsLlmController | ||
|
||
|
||
controller = AwsLlmController( | ||
name="my-dspy", | ||
region="eu-west-1", | ||
) | ||
|
||
|
||
def lambda_handler(event, context): | ||
question = event.get("question") | ||
role = event.get("role", "user") | ||
|
||
response = controller.client.chat.completions.create( | ||
model=controller.model_id, | ||
messages=[{"role": role, "content": question}], | ||
) | ||
|
||
return { | ||
"statusCode": 200, | ||
"headers": {"Content-Type": "application/json"}, | ||
"body": response, | ||
} | ||
|
||
|
||
if __name__ == "__main__": | ||
if not controller.is_runtime_execution: | ||
controller.provision() |
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,5 @@ | ||
-e ../../../damavand | ||
pulumi | ||
boto3 | ||
sagemaker | ||
openai |
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,7 @@ | ||
name: simple-llm-application | ||
runtime: | ||
name: python | ||
options: | ||
toolchain: pip | ||
virtualenv: venv | ||
description: A minimal prompt engineering application that uses open source llm models |
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,53 @@ | ||
import requests | ||
import os | ||
|
||
from damavand.cloud.aws.controllers.llm import AwsLlmController | ||
|
||
|
||
controller = AwsLlmController( | ||
name="my-dspy", | ||
region="eu-west-1", | ||
) | ||
|
||
|
||
def ask(question: str, role: str) -> None: | ||
headers = { | ||
"Content-Type": "application/json", | ||
} | ||
|
||
json_data = { | ||
"messages": [ | ||
{ | ||
"role": role, | ||
"content": question, | ||
}, | ||
], | ||
"parameters": { | ||
"max_new_tokens": 400, | ||
}, | ||
"stream": False, | ||
} | ||
|
||
return requests.post( | ||
controller.chat_completions_url, | ||
headers=headers, | ||
json=json_data, | ||
).json() | ||
|
||
|
||
def lambda_handler(event, context): | ||
question = event.get("question") | ||
role = event.get("role", "user") | ||
return ask(question, role) | ||
|
||
|
||
if __name__ == "__main__": | ||
if not controller.is_runtime_execution: | ||
controller.provision() | ||
else: | ||
event = { | ||
"question": os.environ.get("QUESTION", "What is the capital of France?"), | ||
"role": os.environ.get("ROLE", "user"), | ||
} | ||
context = {} | ||
print(lambda_handler(event, context)) |
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,4 @@ | ||
-e ../../../damavand | ||
pulumi | ||
boto3 | ||
sagemaker |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -1,11 +1,7 @@ | ||
from .base_controller import ApplicationController, runtime, buildtime | ||
from .object_storage import ObjectStorageController | ||
from .spark import SparkController | ||
|
||
__all__ = [ | ||
"ApplicationController", | ||
"ObjectStorageController", | ||
"SparkController", | ||
"runtime", | ||
"buildtime", | ||
"ApplicationController", | ||
] |
Oops, something went wrong.