generated from ks6088ts/template-python
-
Notifications
You must be signed in to change notification settings - Fork 2
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 #108 from ks6088ts-labs/feature/issue-107_promptfl…
…ow-langchain add flex flow with LangChain sample
- Loading branch information
Showing
5 changed files
with
123 additions
and
25 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
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{"question": "What's 4+4?"} | ||
{"question": "What's 4x4?"} |
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,8 @@ | ||
$schema: https://azuremlschemas.azureedge.net/promptflow/latest/Flow.schema.json | ||
entry: main:LangChainRunner | ||
sample: | ||
inputs: | ||
input: What's 2+2? | ||
prediction: What's 2+2? That's an elementary question. The answer you're looking for is that two and two is four. | ||
init: | ||
custom_connection: open_ai_connection |
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,52 @@ | ||
from dataclasses import dataclass | ||
|
||
from langchain_openai import AzureChatOpenAI | ||
from promptflow.client import PFClient | ||
from promptflow.connections import CustomConnection | ||
from promptflow.tracing import trace | ||
|
||
|
||
@dataclass | ||
class Result: | ||
answer: str | ||
|
||
|
||
class LangChainRunner: | ||
def __init__(self, custom_connection: CustomConnection): | ||
# https://python.langchain.com/v0.2/docs/integrations/chat/azure_chat_openai/ | ||
self.llm = AzureChatOpenAI( | ||
temperature=0, | ||
api_key=custom_connection.secrets["api_key"], | ||
api_version=custom_connection.configs["api_version"], | ||
azure_endpoint=custom_connection.configs["api_base"], | ||
model="gpt-4o", | ||
) | ||
|
||
@trace | ||
def __call__( | ||
self, | ||
question: str, | ||
) -> Result: | ||
response = self.llm.invoke( | ||
[ | ||
( | ||
"system", | ||
"You are asking me to do some math, I can help with that.", | ||
), | ||
("human", question), | ||
], | ||
) | ||
return Result(answer=response.content) | ||
|
||
|
||
if __name__ == "__main__": | ||
from promptflow.tracing import start_trace | ||
|
||
start_trace() | ||
pf = PFClient() | ||
connection = pf.connections.get(name="open_ai_connection") | ||
runner = LangChainRunner(custom_connection=connection) | ||
result = runner( | ||
question="What's 2+2?", | ||
) | ||
print(result) |