Skip to content

Commit bf37ea9

Browse files
committed
feat: update bard language support
1 parent 945c452 commit bf37ea9

File tree

2 files changed

+49
-109
lines changed

2 files changed

+49
-109
lines changed

cocoder/aicore/bard_receiver.py

Lines changed: 30 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
# Copyright 2023 parkminwoo
21
import bardapi
3-
from os import environ
2+
import os
43

54

65
def receive_bard_advice(bard_api_key: str, error_message: str) -> str:
@@ -12,91 +11,47 @@ def receive_bard_advice(bard_api_key: str, error_message: str) -> str:
1211
:return: Returns a description and example of the code, the location of the error, and a debugging code example.
1312
:rtype: str
1413
"""
15-
environ["_BARD_API_KEY"] = bard_api_key
16-
17-
if environ.get("_PROMPT_COMMAND") is None:
18-
if environ.get("_BARD_ADVICE_LANG") is None:
14+
prompt_command = os.environ.get("_PROMPT_COMMAND")
15+
bard_advice_lang = os.environ.get("_BARD_ADVICE_LANG")
16+
17+
if prompt_command is None:
18+
if bard_advice_lang == "ko":
1919
input_text = (
20-
f"Give me more explanation for the following error."
21-
f" And if you have a code example and a way to solve it, please suggest it. "
22-
f"Please provide as much information as possible. error is {error_message}."
20+
"다음 에러와 관련된 더 많은 설명을 알려줘. "
21+
"그리고 만약 이 오류와 관련된 코드 예제 및 유용한 자료가 있다면 가능한 많이 알려줘. "
22+
f"오류는 {error_message} 입니다."
23+
)
24+
elif bard_advice_lang == "jp":
25+
input_text = (
26+
"いくつかのリンクまたはコード例を見つけてください。"
27+
"このエラーに関する詳細情報と、この問題に関連する StackOverflow URL を教えてください。 "
28+
f"エラーは {error_message} です。"
2329
)
2430
else:
25-
if environ["_BARD_ADVICE_LANG"] == "ko":
26-
input_text = (
27-
f"다음 에러와 관련된 더 많은 설명을 알려줘. "
28-
f"그리고 만약 이 오류와 관련된 코드 예제 및 유용한 자료가 있다면 가능한 많이 알려줘. "
29-
f"오류는 {error_message} 입니다."
30-
)
31-
elif environ["_BARD_ADVICE_LANG"] == "jp":
32-
input_text = (
33-
f"いくつかのリンクまたはコード例を見つけてください。"
34-
f"このエラーに関する詳細情報と、この問題に関連する StackOverflow URL を教えてください。 "
35-
f"エラーは {error_message} です。"
36-
)
37-
else:
38-
print(
39-
"You can only use ko or jp for the _BARD_ADVICE_LANG variable. "
40-
"Hence, answers will be provided in English."
41-
)
42-
input_text = (
43-
f"Give me more explanation for the following error. "
44-
f"And if you have a code example and a way to solve it, please suggest it. "
45-
f"Please provide as much information as possible. error is {error_message}."
46-
)
31+
input_text = (
32+
"Give me more explanation for the following error. "
33+
"And if you have a code example and a way to solve it, please suggest it. "
34+
f"Please provide as much information as possible. error is {error_message}."
35+
)
4736
else:
48-
input_text = f"{environ['_PROMPT_COMMAND']} error=={error_message}"
37+
input_text = f"{prompt_command} error=={error_message}"
4938

50-
response = bardapi.core.Bard().get_answer(input_text)
51-
advice_msg = response["content"]
39+
bard_api_key = os.environ.get("_BARD_API_KEY", bard_api_key)
40+
bard_lang = os.environ.get("_BARD_ADVICE_LANG", "")
5241

53-
return advice_msg
42+
response = bardapi.core.Bard(token=bard_api_key, language=bard_lang).get_answer(input_text)
5443

44+
return response["content"]
5545

56-
def get_response_bard_advice(bard_api_key: str, error_message: str) -> dict:
46+
47+
def get_response_bard_advice() -> dict:
5748
"""
58-
:param bard_api_key:__Secure-1PSID value of Google bard
59-
:type bard_api_key: str
60-
:param error_message: Error message
61-
:type error_message: str
6249
:return: Returns a response
6350
:rtype: str
6451
"""
65-
environ["_BARD_API_KEY"] = bard_api_key
66-
67-
if environ.get("_PROMPT_COMMAND") is None:
68-
if environ.get("_BARD_ADVICE_LANG") is None:
69-
input_text = (
70-
f"Give me more explanation for the following error."
71-
f" And if you have a code example and a way to solve it, please suggest it. "
72-
f"Please provide as much information as possible. error is {error_message}."
73-
)
74-
else:
75-
if environ["_BARD_ADVICE_LANG"] == "ko":
76-
input_text = (
77-
f"다음 에러와 관련된 더 많은 설명을 알려줘. "
78-
f"그리고 만약 이 오류와 관련된 코드 예제 및 유용한 자료가 있다면 가능한 많이 알려줘. "
79-
f"오류는 {error_message} 입니다."
80-
)
81-
elif environ["_BARD_ADVICE_LANG"] == "jp":
82-
input_text = (
83-
f"いくつかのリンクまたはコード例を見つけてください。"
84-
f"このエラーに関する詳細情報と、この問題に関連する StackOverflow URL を教えてください。 "
85-
f"エラーは {error_message} です。"
86-
)
87-
else:
88-
print(
89-
"You can only use ko or jp for the _BARD_ADVICE_LANG variable. "
90-
"Hence, answers will be provided in English."
91-
)
92-
input_text = (
93-
f"Give me more explanation for the following error. "
94-
f"And if you have a code example and a way to solve it, please suggest it. "
95-
f"Please provide as much information as possible. error is {error_message}."
96-
)
97-
else:
98-
input_text = f"{environ['_PROMPT_COMMAND']} error=={error_message}"
52+
bard_api_key = os.environ.get("_BARD_API_KEY", "")
53+
bard_lang = os.environ.get("_BARD_ADVICE_LANG", "")
9954

100-
response = bardapi.core.Bard().get_answer(input_text)
55+
response = bardapi.core.Bard(token=bard_api_key, language=bard_lang).get_answer("test_message")
10156

10257
return response

cocoder/aicore/openai_receiver.py

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
# Copyright 2023 parkminwoo
21
import openai
3-
from os import environ
2+
import os
43

54

6-
def receive_openai_advice(
7-
open_ai_model: str, open_ai_api: str, error_message: str
8-
) -> str:
9-
"""Receive debugging information about your code from models in open AI.
5+
def receive_openai_advice(open_ai_model: str, open_ai_api: str, error_message: str) -> str:
6+
"""
7+
Receive debugging information about your code from models in open AI.
108
119
:param open_ai_model: model name of open AI
1210
:type open_ai_model: str
@@ -17,52 +15,39 @@ def receive_openai_advice(
1715
:return: Returns a description and example of the code, the location of the error, and a debugging code example.
1816
:rtype: str
1917
"""
20-
2118
openai.api_key = open_ai_api
2219
model_engine = open_ai_model
2320

24-
if environ.get("_PROMPT_COMMAND") is None:
21+
prompt_command = os.environ.get("_PROMPT_COMMAND")
22+
if prompt_command is None:
2523
input_text = (
26-
f"How can I fix this error? Give me short information about next error. "
27-
f"Let me know which code line and which code is incorrect. "
24+
"How can I fix this error? Give me short information about next error. "
25+
"Let me know which code line and which code is incorrect. "
2826
f"And try to make it fix or fix example. error== {error_message}"
2927
)
3028
else:
31-
input_text = f"{environ['_PROMPT_COMMAND']} error=={error_message}"
29+
input_text = f"{prompt_command} error=={error_message}"
30+
3231
resp = openai.ChatCompletion.create(
3332
model=model_engine, messages=[{"role": "user", "content": input_text}]
3433
)
34+
3535
advice_msg = resp["choices"][0]["message"]["content"]
3636
return advice_msg
3737

3838

39-
def get_resp_openai_advice(
40-
open_ai_model: str, open_ai_api: str, error_message: str
41-
) -> dict:
42-
"""Check response of Open AI API status
39+
def get_resp_openai_advice() -> dict:
40+
"""
41+
Check response of Open AI API status
4342
44-
:param open_ai_model: model name of open AI
45-
:type open_ai_model: str
46-
:param open_ai_api: API KEY value of open AI
47-
:type open_ai_api: str
48-
:param error_message: Error message
49-
:type error_message: str
5043
:return: Returns response dict to openai
5144
:rtype: dict
5245
"""
53-
openai.api_key = open_ai_api
54-
model_engine = open_ai_model
55-
56-
if environ.get("_PROMPT_COMMAND") is None:
57-
input_text = (
58-
f"How can I fix this error? Give me short information about next error. "
59-
f"Let me know which code line and which code is incorrect. "
60-
f"and try to make it fix or fix example. error== {error_message}"
61-
)
62-
else:
63-
input_text = f"{environ['_PROMPT_COMMAND']} error=={error_message}"
46+
openai.api_key = os.environ.get("_OPEN_AI_API", "")
47+
model_engine = os.environ.get("_OPEN_AI_MODEL", "")
48+
6449
resp = openai.ChatCompletion.create(
65-
model=model_engine, messages=[{"role": "user", "content": input_text}]
50+
model=model_engine, messages=[{"role": "user", "content": "test"}]
6651
)
67-
52+
6853
return resp

0 commit comments

Comments
 (0)