From df780ba760e7dca311db9e32abd68bd653d7d619 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Wien=C3=B6bst?= Date: Tue, 10 Nov 2026 13:28:24 +0100 Subject: [PATCH] docs(sample-code): add missing sample code for langchain and native client streaming and embedding --- sample-code/README.md | 2 + sample-code/sample_code/amazon.py | 45 +++++++++++++++++++++ sample-code/sample_code/google.py | 15 +++++++ sample-code/sample_code/langchain_amazon.py | 15 +++++++ sample-code/sample_code/langchain_google.py | 15 +++++++ sample-code/sample_code/server.py | 7 +++- 6 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 sample-code/sample_code/langchain_amazon.py create mode 100644 sample-code/sample_code/langchain_google.py diff --git a/sample-code/README.md b/sample-code/README.md index 091699db..d2046051 100644 --- a/sample-code/README.md +++ b/sample-code/README.md @@ -13,7 +13,9 @@ Before running the application, ensure the following prerequisites are met: - `gpt-5.4-nano` - `text-embedding-3-small` - `anthropic--claude-4.6-sonnet` + - `amazon--titan-embed-text` - `gemini-3.5-flash` + - `gemini-embedding` - `sap-rpt-1.6` ## Local Deployment diff --git a/sample-code/sample_code/amazon.py b/sample-code/sample_code/amazon.py index 8d5e2cf8..a1db2f9a 100644 --- a/sample-code/sample_code/amazon.py +++ b/sample-code/sample_code/amazon.py @@ -1,3 +1,6 @@ +import json + +from fastapi.responses import StreamingResponse from gen_ai_hub.proxy.native.amazon import Session @@ -23,3 +26,45 @@ def converse(): messages=conversation, ) return {"result": response["output"]["message"]["content"][0]["text"]} + +def converse_stream(): + """ + Run chat example with streaming response for Claude 4.6 Sonnet. + + Returns: + Streaming response emitting the produced text. + """ + bedrock = Session().client(model_name="anthropic--claude-4.6-sonnet") + conversation = [ + { + "role": "user", + "content": [ + { + "text": "Describe the purpose of a 'Hello World' program in one sentence." + } + ], + } + ] + + def stream(): + response = bedrock.converse_stream( + messages=conversation, + ) + for chunk in response["stream"]: + if "contentBlockDelta" in chunk: + yield chunk["contentBlockDelta"]["delta"]["text"] + + return StreamingResponse(stream(), media_type="text/plain") + +def embedding(): + """ + Run embedding example with Amazon Titan Embed Text. + + Returns: + JSON object containing the embedding. + """ + bedrock = Session().client(model_name="amazon--titan-embed-text") + body = json.dumps({"inputText": "Please recommend books with a theme similar to the movie 'Inception'."}) + response = bedrock.invoke_model(body=body) + response_body = json.loads(response.get("body").read()) + return response_body diff --git a/sample-code/sample_code/google.py b/sample-code/sample_code/google.py index 8177222e..ac1f708f 100644 --- a/sample-code/sample_code/google.py +++ b/sample-code/sample_code/google.py @@ -67,3 +67,18 @@ def add(a: int, b: int) -> int: config=types.GenerateContentConfig(tools=[add]), ) return {"result": response.text} + + +def embedding(): + """ + Run embedding example with Gemini Embedding. + + Returns: + JSON object containing the embedding. + """ + proxy_client = get_proxy_client("gen-ai-hub") + client = Client(proxy_client=proxy_client) + response = client.models.embed_content( + model="gemini-embedding", contents="How many paws are there for a dog?" + ) + return response.embeddings diff --git a/sample-code/sample_code/langchain_amazon.py b/sample-code/sample_code/langchain_amazon.py new file mode 100644 index 00000000..79bb3002 --- /dev/null +++ b/sample-code/sample_code/langchain_amazon.py @@ -0,0 +1,15 @@ +from gen_ai_hub.proxy.langchain.amazon import ChatBedrockConverse +from langchain_core.output_parsers import StrOutputParser + + +def invoke(): + """ + Ask Claude about the capital of Germany. + + Returns: + The answer from Claude. + """ + llm = ChatBedrockConverse(model_name="anthropic--claude-4.6-sonnet") + response = llm.invoke("Where is the capital of Germany?") + parser = StrOutputParser() + return parser.invoke(response) diff --git a/sample-code/sample_code/langchain_google.py b/sample-code/sample_code/langchain_google.py new file mode 100644 index 00000000..955dd935 --- /dev/null +++ b/sample-code/sample_code/langchain_google.py @@ -0,0 +1,15 @@ +from gen_ai_hub.proxy.langchain.google_genai import ChatGoogleGenerativeAI +from langchain_core.output_parsers import StrOutputParser + + +def invoke(): + """ + Ask Gemini about the capital of Germany. + + Returns: + The answer from Gemini. + """ + llm = ChatGoogleGenerativeAI(model="gemini-3.5-flash") + response = llm.invoke("Where is the capital of Germany?") + parser = StrOutputParser() + return parser.invoke(response) diff --git a/sample-code/sample_code/server.py b/sample-code/sample_code/server.py index b7dc9efd..e20fa200 100644 --- a/sample-code/sample_code/server.py +++ b/sample-code/sample_code/server.py @@ -1,7 +1,7 @@ from fastapi import FastAPI, Request from fastapi.responses import JSONResponse -from sample_code import amazon, core, google, grounding, langchain_openai, openai, orchestration, prompt_registry, sap_rpt +from sample_code import amazon, core, google, grounding, langchain_amazon, langchain_google, langchain_openai, openai, orchestration, prompt_registry, sap_rpt app = FastAPI(title="SAP AI Core Python SDK Sample Application") @@ -41,9 +41,12 @@ async def health(): app.get("/google/generate")(google.generate) app.get("/google/generate-stream")(google.generate_stream) app.get("/google/tool-call")(google.tool_call) +app.get("/google/embedding")(google.embedding) # Amazon/Anthropic app.get("/amazon/converse")(amazon.converse) +app.get("/amazon/converse-stream")(amazon.converse_stream) +app.get("/amazon/embedding")(amazon.embedding) # LangChain app.get("/langchain/invoke")(langchain_openai.invoke) @@ -52,6 +55,8 @@ async def health(): app.get("/langchain/tool-chain")(langchain_openai.invoke_tool_chain) app.get("/langchain/rag-chain")(langchain_openai.invoke_rag_chain) app.get("/langchain/stream-chain")(langchain_openai.stream_chain) +app.get("/langchain/amazon-invoke")(langchain_amazon.invoke) +app.get("/langchain/google-invoke")(langchain_google.invoke) # SAP RPT app.get("/sap-rpt/predict-by-rows")(sap_rpt.predict_by_rows)