-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix/feat: langchain templates and models (#293)
* fix(langchain): correctly handles prompt_id and model_id Ref: #285 Signed-off-by: Tomas Dvorak <[email protected]> * feat(langchain): add prompt template example Ref: #285 Signed-off-by: Tomas Dvorak <[email protected]> * feat(langchain): accepts dicts in addition to pydantic models Ref: #285
- Loading branch information
Showing
4 changed files
with
62 additions
and
4 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
examples/extensions/langchain/langchain_generate_with_template.py
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 @@ | ||
"""Use LangChain generation with a custom template.""" | ||
|
||
from dotenv import load_dotenv | ||
|
||
from genai import Client, Credentials | ||
from genai.extensions.langchain import LangChainInterface | ||
from genai.schema import TextGenerationParameters, TextGenerationReturnOptions | ||
|
||
# make sure you have a .env file under genai root with | ||
# GENAI_KEY=<your-genai-key> | ||
# GENAI_API=<genai-api-endpoint> (optional) DEFAULT_API = "https://bam-api.res.ibm.com" | ||
load_dotenv() | ||
|
||
|
||
def heading(text: str) -> str: | ||
"""Helper function for centering text.""" | ||
return "\n" + f" {text} ".center(80, "=") + "\n" | ||
|
||
|
||
client = Client(credentials=Credentials.from_env()) | ||
|
||
prompt_response = client.prompt.create( | ||
name="Recipe Generator Prompt", | ||
model_id="google/flan-t5-xl", | ||
input="Make a short recipe for {{meal}} (use bullet points)", | ||
) | ||
|
||
try: | ||
llm = LangChainInterface( | ||
client=client, | ||
model_id="ibm/granite-13b-instruct-v2", | ||
prompt_id=prompt_response.result.id, | ||
parameters=TextGenerationParameters( | ||
min_new_tokens=100, | ||
max_new_tokens=500, | ||
return_options=TextGenerationReturnOptions(input_text=False, input_tokens=True), | ||
), | ||
data={"meal": "Lasagne"}, | ||
) | ||
for chunk in llm.stream(""): | ||
print(chunk, end="") | ||
finally: | ||
# Delete the prompt if you don't need it | ||
client.prompt.delete(prompt_response.result.id) |
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