Skip to content

Commit a9b30a5

Browse files
committed
Add few-shot ptg implementation
1 parent 61256f0 commit a9b30a5

File tree

14 files changed

+536
-866
lines changed

14 files changed

+536
-866
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
_target_: src.eval.agents.few_shot_agent.FewShotAgent
2+
name: few_shot_fleet
3+
model_name: gpt-4-1106-preview
4+
temperature: 0
5+
model_kwargs:
6+
seed: 76097149
7+
prompt:
8+
_target_: src.eval.prompts.few_shot_prompt.FewShotPrompt
9+
execution_system_prompt_path: configs/template_generation/prompts/fleet_execution_system_prompt.md

configs/template_generation/config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ hydra:
1010
defaults:
1111
- _self_
1212
- data_source: hf
13-
- env: code_engine
14-
- agent: planning
13+
- env: fleet
14+
- agent: few_shot
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
_target_: src.eval.envs.fleet_env.FleetEnv
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
You are Senior Software Architect. You will help to create new project template.
2+
1. Planning step:
3+
1.1 Add code snippet: ```${'\n'}PLANNING${'\n'}```
4+
1.2 Devise a detailed step-by-step action plan for project creation with best practices for language, frameworks and build system.
5+
Specify the task end goal and break down the process into individual actions, using bullet points for clarity.
6+
DO NOT create any code snippets on this step.
7+
8+
2. Structure generation step:
9+
2.1 Add code snippet: ```${'\n'}STRUCTURE_GENERATION${'\n'}```
10+
2.2 Create project structure with directories and files, using tree command output.
11+
12+
3. Content generation step:
13+
2.1 Add code snippet: ```${'\n'}CONTENT_GENERATION${'\n'}```
14+
2.3 For each file in the project add code block with file with filepath as type and content
15+
16+
Strictly follow next rules:
17+
- ALWAYS write example code in the files without any placeholders.
18+
- ALWAYS add README file in the root of the project with short but comprehensive description of project's the structure.
19+
- ALWAYS add README_BUILD file in the root of the project with short but comprehensive description how to build project – describe all build dependencies.
20+
- ALWAYS add language notation 'PROJECT' for project structure
21+
22+
3. Validation step:
23+
3.1 Add code snippet: ```${'\n'}VALIDATION${'\n'}```
24+
3.2 Verbally analyse file content for mistake, errors and incoherence. When file is OK: add code snippet with file path for correct files.
25+
3.3 Change files according to analysis: add file path and then code block with CORRECTED file content
26+
27+
REMEMBER, I don't have fingers to print myself and my work DEPENDS on YOU.
28+
29+
############
30+
Example 1:
31+
```
32+
PLANNING
33+
```
34+
[detailed step-by-step action plan for kotlin gradle project]
35+
36+
```
37+
STRUCTURE_GENERATION
38+
```
39+
40+
```PROJECT
41+
/kotlin-project
42+
├── build.gradle
43+
├── gradle.properties
44+
├── settings.gradle
45+
└── src
46+
├── main
47+
│ ├── kotlin
48+
│ │ └── Main.kt
49+
│ └── resources
50+
└── test
51+
├── kotlin
52+
└── resources
53+
```
54+
55+
```
56+
CONTENT_GENERATION
57+
```
58+
59+
```kotlin-project/build.gradle
60+
plugins {
61+
id 'org.jetbrains.kotlin.jvm' version '1.9.23'
62+
}
63+
64+
group = 'com.example'
65+
version = '1.0-SNAPSHOT'
66+
67+
repositories {
68+
mavenCentral()
69+
}
70+
71+
dependencies {
72+
testImplementation 'org.jetbrains.kotlin:kotlin-test'
73+
}
74+
75+
test {
76+
useJUnitPlatform()
77+
}
78+
kotlin {
79+
jvmToolchain(18)
80+
}
81+
```
82+
83+
```kotlin-project/gradle.properties
84+
kotlin.code.style=official
85+
```
86+
87+
```kotlin-project/settings.gradle
88+
rootProject.name = 'kotlin'
89+
```
90+
91+
```kotlin-project/src/main/kotlin/Main.kt
92+
package com.example
93+
94+
fun main() {
95+
println("Hello World!")
96+
}
97+
```
98+
99+
```
100+
VALIDATION
101+
```
102+
103+
```/kotlin-project/build.gradle
104+
OK
105+
```
106+
107+
```/kotlin-project/gradle.properties
108+
OK
109+
```
110+
111+
```/kotlin-project/settings.gradle
112+
OK
113+
```
114+
115+
```/kotlin-project/src/main/kotlin/Main.kt
116+
OK
117+
```
118+
119+
kotlin-project/README.md file content is missing. Fixing it by adding content.
120+
```/kotlin-project/README.md
121+
[Description of the generated project]
122+
```
123+
124+
kotlin-project/README_BUILD.md file content is missing. Fixing it by adding content.
125+
```/kotlin-project/README_BUILD.md
126+
The project uses
127+
- Gradle build system.
128+
- Java SDK version 18
129+
```
130+
131+
```
132+
PLANNING
133+
```
134+
Example 2:
135+
[detailed step-by-step action plan for groovy maven project]
136+
137+
```
138+
STRUCTURE_GENERATION
139+
```
140+
141+
```PROJECT
142+
/groovy-project
143+
├── pom.xml
144+
├── src
145+
│ └── main
146+
│ └── groovy
147+
│ └── Main.groovy
148+
├── README.md
149+
└── README_BUILD.md
150+
```
151+
152+
```
153+
CONTENT_GENERATION
154+
```
155+
156+
```/groovy-project/pom.xml
157+
<?xml version="1.0" encoding="UTF-8"?>
158+
<project xmlns="http://maven.apache.org/POM/4.0.0"
159+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
160+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
161+
<modelVersion>4.0.0</modelVersion>
162+
163+
<groupId>com.example</groupId>
164+
<artifactId>groovy</artifactId>
165+
<version>1.0-SNAPSHOT</version>
166+
167+
<dependencies>
168+
<dependency>
169+
<groupId>org.apache.groovy</groupId>
170+
<artifactId>groovy-all</artifactId>
171+
<version>4.0.14</version>
172+
<type>pom</type>
173+
</dependency>
174+
</dependencies>
175+
176+
<build>
177+
<plugins>
178+
<plugin>
179+
<groupId>org.codehaus.gmavenplus</groupId>
180+
<artifactId>gmavenplus-plugin</artifactId>
181+
<version>1.13.1</version>
182+
<executions>
183+
<execution>
184+
<goals>
185+
<goal>execute</goal>
186+
</goals>
187+
</execution>
188+
</executions>
189+
<dependencies>
190+
<dependency>
191+
<groupId>org.apache.groovy</groupId>
192+
<artifactId>groovy</artifactId>
193+
<version>4.0.14</version>
194+
<scope>runtime</scope>
195+
</dependency>
196+
</dependencies>
197+
<configuration>
198+
<scripts>
199+
<script>src/main/groovy/Main.groovy</script>
200+
</scripts>
201+
</configuration>
202+
</plugin>
203+
</plugins>
204+
</build>
205+
206+
<properties>
207+
<maven.compiler.source>18</maven.compiler.source>
208+
<maven.compiler.target>18</maven.compiler.target>
209+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
210+
</properties>
211+
212+
</project>
213+
```
214+
215+
```/groovy-project/src/main/groovy/Main.groovy
216+
static void main(String[] args) {
217+
println "Hello world!"
218+
}
219+
```
220+
221+
```/groovy-project/README.md
222+
[Description of the generated project]
223+
```
224+
225+
```/groovy-project/README_BUILD.md
226+
The project uses
227+
- Maven build system
228+
- Java SDK version 18
229+
```
230+
231+
```
232+
VALIDATION
233+
```
234+
235+
```/groovy-project/pom.xml
236+
OK
237+
```
238+
239+
```/groovy-project/README.md
240+
OK
241+
```
242+
243+
```/groovy-project/README_BUILD.md
244+
OK
245+
```

requirements.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ datasets==2.17.1
1212
docker==7.1.0
1313
Flask==3.0.2
1414
pydantic==2.6.3
15-
langchain==0.2.6
16-
langchain-openai==0.1.10
17-
langchain-core==0.2.10
15+
langchain==0.2.7
16+
langchain-openai==0.1.14
17+
langchain-core==0.2.12
1818
python-dotenv==0.21.1
1919
numpy==1.26.4
2020
torch==2.2.1
21-
langsmith==0.1.23
21+
langsmith==0.1.84
2222
# Metrics
2323
nltk==3.8.1
2424
sacrebleu==2.4.1

src/eval/agents/few_shot_agent.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from langchain_openai import ChatOpenAI
2+
from openai import AsyncOpenAI
3+
4+
from src.eval.agents.base_agent import BaseAgent
5+
from src.eval.agents.utils.tokenization_utils import TokenizationUtils
6+
from src.eval.envs.base_env import BaseEnv
7+
from src.eval.prompts.few_shot_prompt import FewShotPrompt
8+
9+
10+
class FewShotAgent(BaseAgent):
11+
12+
def __init__(self, name: str, model_name: str, temperature: int, model_kwargs: dict, prompt: FewShotPrompt):
13+
super().__init__(name)
14+
self._model_name = model_name
15+
self._temperature = temperature
16+
self._model_kwargs = model_kwargs
17+
self._prompt = prompt
18+
19+
async def run(self, env: BaseEnv, user_prompt: str, **kwargs):
20+
execution_prompt = await self._prompt.execution_prompt()
21+
input_messages = [
22+
{
23+
"role": "system",
24+
"content": execution_prompt
25+
},
26+
{
27+
"role": "user",
28+
"content": user_prompt
29+
}
30+
]
31+
#
32+
# tokenization_utils = TokenizationUtils(self._model_name)
33+
#
34+
# client = AsyncOpenAI()
35+
# chat_response = await client.chat.completions.create(
36+
# model=self._model_name,
37+
# messages=tokenization_utils.truncate(input_messages),
38+
# temperature=self._temperature,
39+
# **self._model_kwargs
40+
# )
41+
42+
# description = chat_response.choices[0].message.content
43+
44+
model = ChatOpenAI(model_name=self._model_name, temperature=self._temperature, model_kwargs=self._model_kwargs)
45+
46+
description = model.invoke(execution_prompt + user_prompt).content
47+
48+
await env.run_command('create_template', {'description': description})
49+
50+
return {
51+
"input": input_messages,
52+
"output": description,
53+
"intermediate_steps": []
54+
}

src/eval/agents/utils/openai_utils.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,17 @@
99
from src.eval.agents.utils.tokenization_utils import TokenizationUtils
1010

1111
DEFAULT_MODEL = "gpt-4-1106-preview"
12-
DEFAULT_PROFILE_NAME = "openai-gpt-4"
13-
DEFAULT_MAX_TOKENS = 128000
1412

1513

1614
@retry(wait=wait_random_exponential(multiplier=1, max=40), stop=stop_after_attempt(3))
17-
async def chat_completion_request(client: AsyncOpenAI, messages: list[dict[str, str]], model: str = DEFAULT_MODEL,
18-
max_tokens: int = DEFAULT_MAX_TOKENS, profile_name: str = DEFAULT_PROFILE_NAME,
19-
tools=None, tool_choice=None) -> ChatCompletion:
20-
tokenization_utils = TokenizationUtils(profile_name)
21-
tokens_count = tokenization_utils.count_tokens(messages)
22-
print(f"Tokens: {tokens_count}/{max_tokens}")
15+
async def chat_completion_request(client: AsyncOpenAI, messages: list[dict[str, str]], temperature=1.0, model: str = DEFAULT_MODEL, **model_kwargs) -> ChatCompletion:
16+
tokenization_utils = TokenizationUtils(model)
2317
try:
2418
response = await client.chat.completions.create(
2519
model=model,
26-
messages=tokenization_utils.truncate(messages, max_tokens),
27-
tools=tools,
28-
tool_choice=tool_choice,
20+
messages=tokenization_utils.truncate(messages),
21+
temperature=temperature,
22+
**model_kwargs
2923
)
3024
return response
3125
except Exception as e:

0 commit comments

Comments
 (0)