Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions sample-code/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions sample-code/sample_code/amazon.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import json

from fastapi.responses import StreamingResponse
from gen_ai_hub.proxy.native.amazon import Session


Expand All @@ -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
15 changes: 15 additions & 0 deletions sample-code/sample_code/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions sample-code/sample_code/langchain_amazon.py
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 15 additions & 0 deletions sample-code/sample_code/langchain_google.py
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 6 additions & 1 deletion sample-code/sample_code/server.py
Original file line number Diff line number Diff line change
@@ -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")

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Loading