Skip to content

Commit 5d5c09b

Browse files
tintisimoneclaude
andcommitted
CodeTranslation: Update API client for chat completions and add proxy support
- Migrate from completions API to chat.completions API for better compatibility - Update api_client.py to use system/user message format instead of raw prompts - Add proxy build args to docker-compose.yaml for both backend and frontend - Remove restart policy from frontend service for consistency - Update docstring to reference generic "instruct model" instead of CodeLlama-34b Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
1 parent 4764291 commit 5d5c09b

2 files changed

Lines changed: 37 additions & 31 deletions

File tree

sample_solutions/CodeTranslation/api/services/api_client.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def get_inference_client(self):
3535

3636
def translate_code(self, source_code: str, source_lang: str, target_lang: str) -> str:
3737
"""
38-
Translate code from one language to another using CodeLlama-34b-instruct
38+
Translate code from one language to another using an instruct model.
3939
4040
Args:
4141
source_code: Code to translate
@@ -48,42 +48,41 @@ def translate_code(self, source_code: str, source_lang: str, target_lang: str) -
4848
try:
4949
client = self.get_inference_client()
5050

51-
# Create prompt for code translation
52-
prompt = f"""Translate the following {source_lang} code to {target_lang}.
53-
Only output the translated code without any explanations or markdown formatting.
54-
55-
{source_lang} code:
56-
```
57-
{source_code}
58-
```
59-
60-
{target_lang} code:
61-
```"""
62-
6351
logger.info(f"Translating code from {source_lang} to {target_lang}")
6452

65-
# Use completions endpoint for CodeLlama
66-
response = client.completions.create(
53+
response = client.chat.completions.create(
6754
model=config.INFERENCE_MODEL_NAME,
68-
prompt=prompt,
55+
messages=[
56+
{
57+
"role": "system",
58+
"content": (
59+
f"You are an expert code translator. "
60+
f"Translate {source_lang} code to {target_lang}. "
61+
f"Output only the translated code with no explanations or markdown."
62+
),
63+
},
64+
{
65+
"role": "user",
66+
"content": f"Translate this {source_lang} code to {target_lang}:\n\n{source_code}",
67+
},
68+
],
6969
max_tokens=config.LLM_MAX_TOKENS,
7070
temperature=config.LLM_TEMPERATURE,
71-
stop=["```"] # Stop at closing code block
7271
)
7372

74-
# Handle response structure
75-
if hasattr(response, 'choices') and len(response.choices) > 0:
76-
choice = response.choices[0]
77-
if hasattr(choice, 'text'):
78-
translated_code = choice.text.strip()
79-
logger.info(f"Successfully translated code ({len(translated_code)} characters)")
80-
return translated_code
81-
else:
82-
logger.error(f"Unexpected response structure: {type(choice)}, {choice}")
83-
return ""
84-
else:
85-
logger.error(f"Unexpected response: {type(response)}, {response}")
86-
return ""
73+
if response.choices:
74+
translated_code = response.choices[0].message.content.strip()
75+
# Strip markdown code fences if model wraps output anyway
76+
if translated_code.startswith("```"):
77+
lines = translated_code.splitlines()
78+
translated_code = "\n".join(
79+
lines[1:-1] if lines[-1].strip() == "```" else lines[1:]
80+
).strip()
81+
logger.info(f"Successfully translated code ({len(translated_code)} characters)")
82+
return translated_code
83+
84+
logger.error(f"Empty choices in response: {response}")
85+
return ""
8786
except Exception as e:
8887
logger.error(f"Error translating code: {str(e)}", exc_info=True)
8988
raise

sample_solutions/CodeTranslation/docker-compose.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ services:
33
build:
44
context: ./api
55
dockerfile: Dockerfile
6+
args:
7+
- http_proxy=${http_proxy}
8+
- https_proxy=${https_proxy}
9+
- no_proxy=${no_proxy}
610
container_name: code-trans-backend
711
ports:
812
- "5001:5001"
@@ -32,14 +36,17 @@ services:
3236
build:
3337
context: ./ui
3438
dockerfile: Dockerfile
39+
args:
40+
- http_proxy=${http_proxy}
41+
- https_proxy=${https_proxy}
42+
- no_proxy=${no_proxy}
3543
container_name: code-trans-frontend
3644
ports:
3745
- "3000:8080"
3846
depends_on:
3947
- backend
4048
networks:
4149
- code-trans-network
42-
restart: unless-stopped
4350

4451
networks:
4552
code-trans-network:

0 commit comments

Comments
 (0)